I have a scroll view with many buttons. I want to detect the touches on UIButton inside scrollView but not been able to get them. I tried by subclassing uiscrollview, and uibutton but failed. I want to detect touches on button if user holds that button for a short time, say 2 sec. And then i want to drag the button to place where user can drag it on scroll view. Plz help me.
You don't need to subclass.
Try to attach to your UIButton
a UILongPressGestureRecognizer
.
Have a look here for more info.
Gesture recognizers are available from iOS 3.2 and make very easy all things related to gestures. Here you can find a tutorial.
If you want to support previous versions, you have to resort to a different method:
add UIControlEventTouchUpInside
and UIControlEventTouchDown
actions to your button;
in the touchDown handler start counting time (set a variable with current time);
in touchUp handler stop counting time; measure the difference and if is above your threshold, fire you action.
If this does not work, provide some more information as to why it doesn't, please.
You can set a tag
for each button on the scrollView
, then like @sergio said add a UILongPressGestureRecognizer
(or a uicontrolevent) to each button, so when you are setting the pages in the scrollview you could add:
[button addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
or
UILongPressGestureRecognizer *twoSecPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(someAction:)];
[twoSecPress setMinimumPressDuration:2];
[button addGestureRecognizer:twoSecPress];
[twoSecPress release];
and in your action..
-(IBAction)someAction:(id)sender{
UIButton *button=(UIButton*)sender;
if(button.tag==YOUR_TAG){
//do something
}
}
or
-(void)someAction:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
if ([recognizer.view isKindOfClass:[UIButton class]]) {
UIButton *tmpButt=(UIButton *)recognizer.view;
NSLog(@"%d", tmpButt.tag);
}
}
(obviously add UIGestureRecognizerDelegate on your.h)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.