★ TouchArcade needs your help. Click here to support us on Patreon.

Checking if a button is down

06-03-2009, 02:01 AM
#1
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
Checking if a button is down

Hey Guys,
I wanted to know how do you check if you are pressing a button? I wanted to set off an action as long as the button is down, but now I can only set it off it I press it! If I press it once, the action is only done once. I want to do it as long as I am pressing the button.
06-03-2009, 03:37 AM
#2
Joined: Apr 2009
Location: Munich
Posts: 384
Send a message via Skype™ to rdklein
best is to use the touchbegin, touchend and set flags when the messages arriv in the veiw, probably you have to check with pointinrect if you hit the real button at that time.

RadioactivityCounter on App Store developer of this App RadioactivityCounter 2012
Ports of Call on App Store developer of this game 1986-2009,
06-03-2009, 03:43 AM
#3
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
Quote:
Originally Posted by rdklein View Post
best is to use the touchbegin, touchend and set flags when the messages arriv in the veiw, probably you have to check with pointinrect if you hit the real button at that time.
I want to move an image to the right if I press it. The problem now is that it only moves when I press it. I don't really understand what you are talking about, but thanks. It's not a message, I want to move an image.
06-03-2009, 04:28 AM
#4
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
Quote:
Originally Posted by froggy1233 View Post
I want to move an image to the right if I press it. The problem now is that it only moves when I press it. I don't really understand what you are talking about, but thanks. It's not a message, I want to move an image.
I have a feeling one of the example apps provided by Apple might implement this functionality... anyone recall? It's been a while since I looked at the Apple example apps.

rdklein is referring to the touchesBegan:withEvent: and touchesEnded:withEvent: messages which are inherited by UIButton objects from the UIResponder class. Check the documentation for the UIResponder class.

Ah just found the Apple example app, it's called MoveMe so hunt out the source for that and that should help you along the way.

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
06-03-2009, 04:58 AM
#5
Joined: Jan 2009
Location: App Tech Studios, USA
Posts: 1,363
If you want to do something when you press the button and something else when you unpress, just make 2 IBActions, go into IB, select your button, right click, scroll down on that black/grey pop-up thing to Touch Down and drag it to the press down method and do the same thing only find Touch Up Inside and drag it to the touch up method.


I hope this is helpful!
06-03-2009, 05:50 AM
#6
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
Quote:
Originally Posted by WellSpentYouth View Post
If you want to do something when you press the button and something else when you unpress, just make 2 IBActions, go into IB, select your button, right click, scroll down on that black/grey pop-up thing to Touch Down and drag it to the press down method and do the same thing only find Touch Up Inside and drag it to the touch up method.


I hope this is helpful!
Okay, so when I touch down on the button, I start an NSTimer which sets off an action to move the ball right. Now, when I touch up inside, I want to stop that timer. Any way to do this?

Last edited by kohjingyu; 06-04-2009 at 12:56 AM.