iPhone SDK 3.2 UIGestureRecognizer干扰UIView动画?

时间:2010-04-23 16:34:44

标签: iphone animation iphone-sdk-3.2 gesture-recognition gesture

手势识别器和动画的UIView类方法是否存在已知问题?

我遇到了来自UIGestureRecognizer回调的UIImageView上的一系列动画问题。如果动画序列是从像TouchUpInside这样的标准回调开始的,那么动画效果很好。如果它是通过UILongPressGestureRecognizer启动的,那么第一个动画会跳到结尾,第二个动画会立即开始。

这是一个说明我的问题的示例。在项目的.xib中,我有一个连接到viewToMove IBOutlet的UIImageView。我还有一个连接到startButton IBOutlet的UIButton,我已将其TouchUpInside动作连接到startButtonClicked IBAction。 TouchUpInside动作按我的意愿工作,但longPressGestureRecognizer在大约半秒后跳到第一个动画的结尾。当我NSLog第二个动画(animateTo200)时,我可以看到它在长按开始动画时被调用两次,但只有当按钮的TouchUpInside动作开始动画时才被调用一次。

- (void)viewDidLoad {
[super viewDidLoad];

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startButtonClicked)];
NSArray *recognizerArray = [[NSArray alloc] initWithObjects:longPressRecognizer, nil];
[startButton setGestureRecognizers:recognizerArray];

[longPressRecognizer release];
[recognizerArray release];
}

-(IBAction)startButtonClicked {

if (viewToMove.center.x < 150) {
    [self animateTo200:@"Right to left" finished:nil context:nil];
} else {
    [self animateTo100:@"Right to left" finished:nil context:nil];
}
}

-(void)animateTo100:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:@"Right to left" context:nil];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animateTo200:finished:context:)];
viewToMove.center = CGPointMake(100.0, 100.0);
[UIView commitAnimations];          
}

-(void)animateTo200:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:@"Left to right" context:nil];
[UIView setAnimationDuration:4];
viewToMove.center = CGPointMake(200.0, 200.0);
[UIView commitAnimations];          
}

1 个答案:

答案 0 :(得分:4)

您应该将startButtonClicked的签名更改为- (void)startButtonClicked:(UIGestureRegognizer *)gestureRecognizer,然后在方法中查询手势识别器的state属性。手势识别器将使用不同的状态多次调用其操作方法(例如UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded)。