3
我通常使用這種方法來實現UIButton的可移動;如何使UIButton拖動但不觸發點擊事件
[button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];
但它會同時觸發touchUpInside事件,我需要touchUpInside事件來做其他事情。
那麼有誰知道如何避免這種情況?
非常感謝;
使用下面的代碼來解決它;
[button addTarget:self action:@selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:@selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
- (void) touchUpInside :(UIControl*)c withEvent:ev{
NSLog(@"touch Up inside");
if (count) {
NSLog(@"here I can do something about clicking event");
}
}
- (void) touchDown:(UIControl*)c withEvent:ev{
NSLog(@"touch Down");
count = YES;
}
-(void) touchDragInside:(UIControl*)c withEvent:ev{
NSLog(@"touch drag inside");
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
count = NO;
}
檢查出可能[重複爲此](http://stackoverflow.com/questions/15588604/how-to-implement-two-ibactions-in-uibutton-without-overlap) 它會幫助你。 – 2013-04-20 09:08:16
把它解決了。我只是在touchUpInside事件中製作一面旗幟。 – zedzhao 2013-04-21 15:06:57