Yiyi
Yiyi

Reputation: 23

How to make UIButton respond only to touch, not drag

Take a look at this method:

[textButton addTarget:self action:@selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchDown];

When the button is touched, it calls articleModalWillAppear:. That works well. The problem is the button also calls the action when it is dragged. (The button is big and contains a text paragraph)

I put six such buttons as subviews of UIScrollView (with UIPageControl). It works well when dragging UIScrollView horizontally. But it pops up modal views when dragging vertically because when a finger dragging across a button, the button considers it as a touch, and the touch calls articleModalWillAppear:.

If you still don't understand my problem, think about the New York Times iPad app. You can drag the pages horizontally. You can touch an article description to go to the full article view. But nothing happens when you drag vertically inside an article description. How to achieve this?

Upvotes: 0

Views: 787

Answers (2)

Thomas Clayson
Thomas Clayson

Reputation: 29935

use forControlEvents:UIControlEventTouchUpInside

That only registers a click event when a user touches down inside the button and then touches up in the same button, and ignores drags.

Upvotes: 1

Joost
Joost

Reputation: 10413

Use a different event e.g.:

[textButton addTarget:self action:@selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchUpInside];

UIControlEventTouchUpInside is the most generally used in this case.

Upvotes: 1

Related Questions