Hello, I got one pressing issue which can prevent my app from ever happening. I have a button which on "touch up inside" starts a UIImageView animation.
What I'd like to have happen is create a custom handler for that button which detects how long the user kept the button pressed and play a different UIImageView animation while that is happening (it can happen on the same UIImageView or on a superimposed one, after making the "background UIImageView" invisible). Ideally, upon button release I'd like to get back to the original animation.
Any ideas and suggestions are appreciated - lemme know what you guys think.
Instead of using an IBAction touch, you can use a touchesBegan and touchesEnded event.
This will allow you to detect the time they held their touch down inside the button.
Right, but using a timer is out of the question because the animation doesn't happen after the user releases the button, but while the button is pressed.
Right, but using a timer is out of the question because the animation doesn't happen after the user releases the button, but while the button is pressed.
In that case, touchesBegan would set the animation to start in an infinite loop.
touchesEnded would set the animation to stop.
__________________ Water Your Body (ON SALE $0.99)
Top Paid in Health & Fitness!
Is this what you meant by subclassing the button? Provided this code is right, how can I add the touchesBegan as the @selector for this button?
Once again, I really appreciate the help.
Code:
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button1.frame = CGRectMake(5, 5, 60, 60);
//set the button's title
[button1 setTitle:@"title"
forState:UIControlStateNormal];
//listen for clicks
[button1 addTarget:self
action:@selector(changeMainView:) // change this function
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[self.view addSubview:button1];
Last edited by blackoutq3; 12-01-2009 at 02:37 PM.
Repeating your answer (period and all ) doesn't do too much good. I clearly am a beginner who needs help. What do you mean by subclassing a UIButton? Have it sit in its own class? (seems pointless)
Last edited by blackoutq3; 12-01-2009 at 03:49 PM.
"subclassing" means defining your own subclass of an NSObject which takes on all the behavior of the object (UIButton, in this case) plus whatever funcionality you add/override.
in this case i'm suggesting you create your own subclass of UIButton which will allow you to override the touch methods (touchesBegan, touchesMoved, etc) of the button. then you can calculate the time between touchesBegan and touchesEnded. you can then pass this info to the button's delegate (your view controller).
this might be too advanced if you're totally new to this, so if words like "subclass" and "delegate" make you go WTF MAN then you should start with an objective-c primer. the Stanford iPhone development course (available for free on iTunes U right) is a great resource for a beginner to get familiar with Obj-C and Cocoa Touch.
btw your response to bravetarget seems to indicate you don't understand how touches are handled and passed to views on the iPhone. no offense, but you should probably RTFM a little.
one last thought, look into the different UIControlEvents available to you. TouchUpInside is only one of many events that exist for a UIButton. There is probably an event that fires as soon as the finger touches the button, and another that fires when the finger is lifted. define a "buttonTouched" and a "buttonReleased" method, assign them to the appropriate button events, and...see where i'm going with that?