iphone开发:在内部同时使用touch-down和touch-up



在我的应用程序中,我使用一个按钮,并为它们分配了两种方法——一种是在向下触摸时工作(按钮图像发生更改),另一种是当在内部触摸时工作。简单地说,如果你想打开一个视图,你按下按钮,但当你触摸按钮时,图像会发生变化,当你举起手指后,另一个视图就会打开。我的问题是,如果你按下按钮,图像会发生变化,但如果你把手指移到远离按钮的某个地方,内部的触摸就不会像预期的那样工作。但问题是,图像会一直停留在过度版本,因为向下触摸只会触发一次。我该怎么办?感谢

您可以在控制状态touchDragOutsidetouchDragExit中处理此问题,具体取决于您希望它做什么。使用touchDragOutside,您可以检测用户何时在按钮内部向下触摸并拖动手指而不离开按钮的可触摸边界,而touchDragExit则检测用户何时拖动到按钮可触摸边界之外。

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside];

我建议您使用UIButton对象的这种方法来更改图像。

- (void)setImage:(UIImage *)image forState:(UIControlState)state

你可以在这里看到该州的所有选项http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

我会使用状态UIControlStateNormal和UIControlStateHighlighted作为您的目标。

我自己也遇到过这个问题,我们主要使用以下事件:-

//此事件运行良好并触发

[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];
}
}

最新更新