0

在我的应用程序中,我使用了一个按钮,并为它们分配了两种方法,一种在您按下时工作(按钮图像已更改),另一种在您在内部触摸时工作(打开另一个视图)。很简单,如果你想打开一个视图,你按下按钮,但是当你触摸按钮时,图像会改变,当你抬起手指后,另一个视图就会打开。我的问题是,如果您按下按钮,图像就会改变,但是如果您将手指移到远离按钮的某处,则内部触摸无法正常工作。但问题是图像坚持其过度版本,因为触地触发一次。我该怎么办?谢谢

4

3 回答 3

3

您可以在控制状态下touchDragOutsidetouchDragExit根据您想要它做什么来处理它。使用touchDragOutside您可以检测用户何时在按钮内部触摸并在不离开按钮可触摸边界的情况下将手指拖开,并touchDragExit检测用户何时将其拖动到按钮可触摸边界之外。

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside];
于 2012-09-06T15:46:13.000 回答
2

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

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

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

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

于 2012-09-06T15:53:19.270 回答
0

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

// 这个事件工作正常并触发

[按钮 addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];

// 这根本不会触发

[按钮 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];
}
}
于 2014-02-19T07:24:52.113 回答