iphone开发:使用触摸并同时触摸内

问题描述:

在我的应用程序中,我使用一个按钮,我分配了两种方法,他们之一,当你触摸(按钮图像被改变)工作,另一个工作时,当你触摸在里面(另一个视图被打开)。简单地说,如果你想打开一个视图,你可以按下按钮,但是当你触摸按钮时,图像会被改变,并且在你抬起手指之后,另一个视图被打开。我的问题是,如果按下按钮图像被更改,但是如果您将手指移动到远离按钮的位置,则内部触摸不起来,因为它应该是。但问题在于图像坚持其超版本,因为触发一次就会触发。我该怎么办?谢谢iphone开发:使用触摸并同时触摸内

您可以在控制状态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为你的目标。

我面临这个问题我自己,主要是我们使用这些事件: -

//此事件正常工作和火灾

[按钮addTarget:自我行动:@selector(压紧)forControlEvents:UIControlEventTouchDown ]。

//这不火都

[按钮addTarget:自我行动:@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]; 
} 
}