【问题标题】:iphone development: using touch down and touch up inside simultaneoslyiphone开发:同时使用touch down和touch up inside
【发布时间】:2012-08-31 11:39:36
【问题描述】:
在我的应用程序中,我使用了一个按钮,并为它们分配了两种方法,一种在您按下时工作(按钮图像已更改),另一种在您在内部触摸时工作(打开另一个视图)。很简单,如果你想打开一个视图,你按下按钮,但是当你触摸按钮时,图像会改变,当你抬起手指后,另一个视图就会打开。我的问题是,如果您按下按钮,图像就会改变,但是如果您将手指从按钮上移到某个地方,则内部触摸无法正常工作。但问题是图像坚持其过度版本,因为触地被触发一次。我该怎么办?谢谢
【问题讨论】:
标签:
iphone
objective-c
ios
cocoa-touch
user-interaction
【解决方案1】:
您可以在控制状态touchDragOutside 或touchDragExit 中处理此问题,具体取决于您希望它执行的操作。使用touchDragOutside,您可以检测用户何时在按钮内部按下并拖动手指而不离开按钮的可触摸边界,touchDragExit 检测用户何时将其拖动到按钮可触摸边界之外。
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside];
【解决方案3】:
我自己也遇到过这个问题,我们大多使用这些事件:-
// 这个事件可以正常工作并触发
[button addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
// 这根本不会触发
[button addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];
解决方法:-
使用长按手势识别器:-
UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[button addGestureRecognizer:btn_LongPress_gesture];
手势的实现:-
- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{
//as you hold the button this would fire
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self someMethod];
}
//as you release the button this would fire
if (recognizer.state == UIGestureRecognizerStateEnded) {
[self someMethod];
}
}