iPhone Programming - How to create a 'hold down' button

preview_player
Показать описание
An Xcode 4 tutorial teaching you how to create a "hold down" button so when they are "holding" it down the action carries on and when they let go the action stops.

Main Code:

In the .h:
IBOutlet UIImageView *image;
NSTimer *goUp;
NSTimer *goDown;
NSTimer *goLeft;
NSTimer *goRight;

}

-(IBAction)down; //Conected to "touch down"
-(IBAction)stopDown; //Connected tp "touchUpInside"
-(IBAction)up; //Conected to "touch down"
-(IBAction)stopUp; //Connected tp "touchUpInside"
-(IBAction)left; //Conected to "touch down"
-(IBAction)stopLeft; //Connected tp "touchUpInside"
-(IBAction)right; //Conected to "touch down"
-(IBAction)stopRight; //Connected tp "touchUpInside"

-(void)goDown;
-(void)goUp;
-(void)goLeft;
-(void)goRight;

In the .m
-(IBAction)down {
if (goDown == nil) {
}
}

-(IBAction)stopDown {
[goDown invalidate];
goDown = nil;
}

-(IBAction)up {
if (goUp == nil) {
}
}

-(IBAction)stopUp {
[goUp invalidate];
goUp = nil;
}

-(IBAction)left {
if (goLeft == nil) {
}
}

-(IBAction)stopLeft {
[goLeft invalidate];
goLeft = nil;
}

-(IBAction)right {
if (goRight == nil) {
}
}

-(IBAction)stopRight {
[goRight invalidate];
goRight = nil;
}

-(void)goDown {
}

-(void)goUp {
}

-(void)goLeft {
}

-(void)goRight {
}



Рекомендации по теме
Комментарии
Автор

I wanted to figure out how to do this earlier this year, but couldn't and the tutorials I found were no help at all - thanks to you Milmer I have now updated one of my apps and the user interface is transformed! Much easier to use and less frustrating. Cheers!

Subscribed

CoachCasson
Автор

thanks so much! i used this to make a move left and right button for my platforming game :DD:D:D befoe i was using touchesbegan and touches ended to move but i prefer arrow buttons on one side instead

HidingInMyRoom
Автор

You are a saint and have gained another subscriber. THANK YOU VERY

derrickmu
Автор

Thank you very much! This video helped me a lot!

sportsaddict
Автор

great job, you are the best
would it be easy to make it so that the image stops at the end of the view
thanks in advance

mansports
Автор

it says on the line [goDown invalidate]; that goDown has not been declared

pashapa
join shbcf.ru