做某事当视图接收手势时

时间:2015-03-21 01:09:08

标签: ios objective-c uigesturerecognizer

我希望在收到backgroundColor时更改视图UILongPressGesture,我该怎样才能正确完成?使用下面的代码,一切都会永远冻结,即使我抬起手指也不会解冻。

- (void)longPress:(UILongPressGestureRecognizer *)gesture {
 while ([gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [self.Row2View setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.2]];
    }
}

编辑我需要的是:当UILongPressGesture改变颜色时,当手指抬起屏幕时会改变颜色。

3 个答案:

答案 0 :(得分:2)

因为"而"块永远不会破裂!只需更改" while"阻止"如果"!

答案 1 :(得分:1)

你用的时候!当进入循环时,条件将始终为真!

首先,将更改为。然后根据苹果文件:

  

长按手势是连续的。当指定时间段(UIGestureRecognizerStateBegan)按下允许手指的数量(numberOfTouchesRequired)并且触摸不超出允许的移动范围时,手势开始(minimumPressDuration)(allowableMovementUIGestureRecognizerStateEnded)。每当手指移动时,手势识别器都会转换为更改状态;当任何手指抬起时,手势识别器会结束(if ([gesture state] == UIGestureRecognizerStateBegan) { [self.Row2View setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.2]]; } )。

我认为你应该使用开始状态(当手势被识别时)或结束状态(当识别出的手势释放时)。如果你不想在触摸移动时连续调用方法。

if ([gesture state] == UIGestureRecognizerStateBegan) {
    Row2ViewOriginColor = self.Row2View.backgroundColor; // you can declare this var in the class.
    [self.Row2View setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.2]];
}
else if ([gesture state] == UIGestureRecognizerStateEnded ||
         [gesture state] == UIGestureRecognizerStateCancelled) {
    [self.Row2View setBackgroundColor: Row2ViewOriginColor];
}

--- 编辑 ---

根据您的描述,您希望在触摸时更改颜色并在发布时恢复。所以它将是:

{{1}}

答案 2 :(得分:0)

添加属性

@property (copy) UIColor *initialBackgroundColor;

长按

- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
    if ([gesture state] == UIGestureRecognizerStateBegan)
    {
        self.initialBackgroundColor = self.Row2View.backgroundColor;
        [self.Row2View setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.2]];
    }
    else if ([gesture state] == UIGestureRecognizerStateEnded ||
             [gesture state] == UIGestureRecognizerStateCancelled ||
             [gesture state] == UIGestureRecognizerStateFailed)
    {
        [self.Row2View setBackgroundColor:self.initialBackgroundColor];
    }
}