在视图接收到手势时做某事
Do something While view receives gestures
我想在接收 UILongPressGesture
时更改视图 backgroundColor
,如何才能正确执行?使用下面的代码,一切都会永远冻结,即使我抬起手指也不会解冻。
- (void)longPress:(UILongPressGestureRecognizer *)gesture {
while ([gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[self.Row2View setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.2]];
}
}
编辑我需要的是:当UILongPressGesture
改变颜色时,当手指抬起屏幕时改变颜色。
你用while!当进入循环时,条件永远为真!
首先,将 while 更改为 if。然后根据苹果文档:
Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan
) when the number of allowable fingers (numberOfTouchesRequired
) have been pressed for the specified period (minimumPressDuration
) and the touches do not move beyond the allowable range of movement (allowableMovement
). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded
) when any of the fingers are lifted.
我认为你应该使用开始状态(当手势被识别时)或结束状态(当识别的手势释放时)。如果你不想在触摸移动时连续调用方法。
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];
}
因为"while"块永远不会破!只需将 "while" 块更改为 "if"!
添加一个属性
@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];
}
}
我想在接收 UILongPressGesture
时更改视图 backgroundColor
,如何才能正确执行?使用下面的代码,一切都会永远冻结,即使我抬起手指也不会解冻。
- (void)longPress:(UILongPressGestureRecognizer *)gesture {
while ([gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[self.Row2View setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.2]];
}
}
编辑我需要的是:当UILongPressGesture
改变颜色时,当手指抬起屏幕时改变颜色。
你用while!当进入循环时,条件永远为真!
首先,将 while 更改为 if。然后根据苹果文档:
Long-press gestures are continuous. The gesture begins (
UIGestureRecognizerStateBegan
) when the number of allowable fingers (numberOfTouchesRequired
) have been pressed for the specified period (minimumPressDuration
) and the touches do not move beyond the allowable range of movement (allowableMovement
). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded
) when any of the fingers are lifted.
我认为你应该使用开始状态(当手势被识别时)或结束状态(当识别的手势释放时)。如果你不想在触摸移动时连续调用方法。
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];
}
因为"while"块永远不会破!只需将 "while" 块更改为 "if"!
添加一个属性
@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];
}
}