“保持”UIButton行为 – “触摸取消”控制状态阻止进一步的控制状态

我有一个子视图button(为了说话的缘故,子视图是一个红色的方块),当用户按下button的红色方块animation半透明。

我有连接到这个方法的button:

-(IBAction)peekToggle:(id)sendr{ NSLog(@"TOGGLE"); if(self.view.alpha ==1)self.view.alpha = 0.1; else self.view.alpha = 1; } 

通过行为: touch up insidetouch up outsidetouch down 。 所以当我按住button的红色框变成半透明,当我释放我的手指,它返回到不透明。 这最初工作正常,但是,如果我按住button超过1秒,button不会注册touch up (释放的手指)。

注意:在父视图(父视图的子视图父母不是父button)上有一个longPressGestureRecogniser,但没有被触发(预期)。

林非常确定,我长按button被注册为touch cancel ,然后使触摸事件无效。

我怎样才能防止/解决这个问题?

我可以停止touch Cancel触发? (即使我已经注册了控制状态,这个事件似乎会触发),或者在touch Cancel事件中,告诉button保持/开始注册事件?

解:

完全删除了IBActions,并将UILongPressGestureRecognizer添加到button中的时间非常短。

  UILongPressGestureRecognizer * recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; recognizer.minimumPressDuration = 0.1; [self.peekButton.view addGestureRecognizer:recognizer]; [recognizer release]; 

然后在gr的select器中,阅读gr的状态:

  - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer{ //1 = start if(gestureRecognizer.state==1 || gestureRecognizer.state==3)[self peekToggle]; //3=end } 

如果你认为这是你的问题,你可以尝试覆盖- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event ,看看你是否有任何活动。

您可以使用UIGestureRecognizerDelegate接口来微调手势识别器被触发的时间。