Clickable UISlider - objective-c

I'd like to make my UISlider clickable - to change the value on click to "blank space", e.g. It's set to zero, I click in the middle of the slider and it will "jump" to the middle. Is there any way how to do this?

I want to propose something similar as jrtc27, but without subclassing: add a UI(Tap)GestureRecognizer to the slider.
UISlider *slider = [[[UISlider alloc] init] autorelease];
//configure slider
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sliderTapped:)] autorelease];
[slider addGestureRecognizer:tapGestureRecognizer];
- (void)sliderTapped:(UIGestureRecognizer *)gestureRecognizer {
UISlider *slider = (UISlider *) gestureRecognizer.view;
//setValue
}

Subclass it and then alter the -touchesBegan method. If the touch is sufficiently far away, call -setValue:, else call the super implementation (if you want it to always jump, you can just always call -setValue:).

Related

Click Event on UIImageView programmatically

I'am displaying a collection of images from code,
This is my code:
UIImageView *addview;
myimgeview *addimageview =[[myimgeview alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#%d",PhotoName,i]] text:[textArray objectAtIndex:i]];
I want to handle the touch event on the addimageView programmatically.
I would suggest different approach. Add tap gesture recognizer to the imageview. That is probably easiest solutions. If you have addImageView method that adds image view to view then, go on like this,
- (void)addImageView
{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,100,100);
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self selector:#selector(tapped:)];
[imageView addGestureRecognizer:tap];
}
- (void)tapped:(UITapGestureRecognizer*)tap
{
NSLog(#"%#", tap.view);
}
You have to subclass it and override -touchesBegan: method or you can add gesture recognizer to this object. Here is documentation about that:
Event Handling Guide for iOS
Good way is to subclass this class and add gesture recognizer inside the class.

NSMutableArray objects being lost

I hope this makes sense...I have an NSMutableArray that I'm attempting to store multiple UIScrollView's in. Each UIScrollView is going to have multiple images and the ultimate goal is to be able to allow the user to swipe vertically for categories (each instance of UIScrollView) and horizontally for each image in that category (each instance of UIImageView). For now, until I get this code to work, I'm just creating 1 UIScrollView with 1 image and I'm adding that to scrollViewManager. This seems to add everything correctly, but when I leave this function, game over. My Array is empty. I don't understand. Am I supposed to do some sort of deep copy when I add to the Array so it doesn't get destroyed. Perhaps I'll figure it out when I wake up tomorrow, but for now, I'd like to break everything in sight. Thanks, in advance!
EDIT: Sorry for lack of details, I posted this rather late. My project is using ARC, so I'm not releasing scrollView anywhere. Here is the declaration for scrollViewManager in my header file:
#property (nonatomic, retain) NSMutableArray *scrollViewManager;
Here is the code I use to retrieve my scrollView:
UIScrollView *test = (UIScrollView*) [self.scrollViewManager objectAtIndex: 0];
And here is the code to initialize the scrollView and array:
scrollViewManager = [[NSMutableArray alloc] initWithCapacity: 1];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSString *imageName = #"pic1.png";//[NSString stringWithFormat:#"pic%d.jpg", i+1];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = 400;
rect.size.width = 300;
imageView.frame = rect;
imageView.tag = 1; // tag our images for later use when we place them in serial fashion
[scrollView addSubview:imageView];
[self.scrollViewManager addObject: scrollView];
There are some things that are kind of vague. For example:
Are you using ARC, if not and you are releasing your scrollView anywhere?
Are you using the auto-create-synthesize thing?
What type of property is your scrollViewManager (weak, strong, etc).
If you are creating your ivars automatically when defining a property, your ivar should be called _scrollViewManager instead of scrollViewManager.
The use of self.scrollViewManager is correct when using this method.
I always do this kind of stuff the other way around. When I have a property like this:
#property (nonatomic, strong) NSMutableArray* myArray;
I always initialize them using the self keyword:
self.myArray = [NSMutableArray new];
And when modifying it, I always use the generated ivar:
[_myArray addObject:myFineObject];
I think this is something I kept using when moving from non-ARC to ARC. Not sure if it still the way to go, but it works like expected.

UIButton does not detect touch events when clicked

I've created a tableview that has custom cells (I've subclassed UITableViewCell).
I'm creating several UIButtons in it. Declaring this in the .h file:
#property (nonatomic, strong) UIButton *myButton;
and this in the .m file:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(-100.0, 285.0, 60.0, 30.0)];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
myButton.backgroundColor = [UIColor clearColor];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setTitle:#"text" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:myButton];
I'm moving the cell to the right using UIPanGestureRecognizer and then the UIButton is revealed (notice that it is in a negative X index).I can see it but it is not clickable (I guess it is not in the view any more).
Any suggestion about how to make it clickable?
More details:
In the init of the cell, I'm doing:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
panGesture.delegate = self;
[self.contentView addGestureRecognizer:panGesture];
Then I implemented the gesture:
-(void)handlePanGesture:(UIPanGestureRecognizer *)sender {
CGPoint translate = [sender translationInView:self.contentView];
CGRect currentFrame = self.contentView.frame;
switch ([sender state]) {
case UIGestureRecognizerStateChanged:
currentFrame.origin.x = translate.x;
self.contentView.frame = currentFrame;
break;
default:
break;
}
}
When the frame moves to the right - the button is revealed. But not clickable...
If a button's frame is outside that of its parent's frame, it will not register touches. In this case, you mention that your button stops working when its frame's x location is set to -100. This makes sense because it is outside the touch area.
Your pan gesture you implemented moves the cell's view, so while the button may appear on screen, it is still OUTSIDE of the cell's view, hence it cannot receive touches.
One possible suggestion for fixing this would be to make your cells bigger from the start. So for example, if a cell's frame size is currently 0,0,320,44, you could instead make it -100,0,420,44 and place the contents of your cell accordingly. Now, your button's frame would change to 0,0,x,y and it would be inside of the cell's view and able to receive touches.

How to make a UIActivityIndicatorView in a UIToolbar tappable

I'm using this code to insert an UIActivityIndicatorView to my toolbar
-(void)addActivityIndicatorToToolbar {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
activityIndicator.userInteractionEnabled = YES;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[activityIndicator startAnimating];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
barButton.target = self;
barButton.action = #selector(playButtonPressed:);
NSMutableArray *toolbarItemsMutable = [self.toolbar.items mutableCopy];
[toolbarItemsMutable replaceObjectAtIndex:0 withObject:barButton];
self.toolbar.items = toolbarItemsMutable;
}
However, when I tap the UIActivityIndicatorView the action (playButtonPressed) is not performed.
How can I correct this?
It seems more likely that you want a button with an activity indicator inside it. You can do this by creating a button with a custom view as described in this post. Then you can set the action of this button as normal, and you'll probably want to retain a reference to the activity indicator to start and stop it.
I ended up implementing a poor man's solution by adding an extra view on top of the activityIndicatorView with a gestureRecognizer.
This is a quite old question but why don't you directly add a UITapGestureRecognizer instance to your UIActivityIndicatorView instance ? (works fine on iOS 8.2, I didn't test yet on previous versions).

UIkeyboard popup with uitextfield on top (pic included)

I am trying to achieve this type of effect where a barbutton is pressed and uikeyboard pops up and right above it brings a uitextfield with it. Please see image attached. Can someone point me to the right about how I can do this?
Thanks.
Use UIToolbar or UIActionSheet
Setup a textField as an inputAccessoryView for the keyboard.
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 260, 30)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:textField, nil]];
masterTextField.inputAccessoryView = keyboardDoneButtonView;
// where masterTextField is the textField is the one you tap, and the keyboard rises up along with the small textField.
masterTextField.returnKeyType = UIReturnKeyDone;
[masterTextField setKeyboardType:UIKeyboardTypeDefault];
update:
For a button, put the above code in the (IBAction) of the button.
And instead of masterTextField, create a UIView and add it as a SubView to your view.