iOS滑动动画两次出现不足之处

时间:2012-09-12 12:49:36

标签: objective-c ios uiimageview core-animation

我正在使用带有ARC的Xcode 4.4开发一个基于页面的iPhone应用程序,并且已经坚持了一段时间。在某个页面上,UIImageView手指需要向上滑动并指向某个东西,然后向左滑动离开屏幕。当我运行一次时,这可以正常工作,但当我翻页并返回时 - 现在有2个手指滑动约0.5秒与彼此不同步。

以下是代码:

-(void)fingerSwipeUp:(UIImageView *)imageView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [imageView setCenter:CGPointMake(213.75,355.5)];
    [UIView commitAnimations];
}

-(void)fingerSwipeLeft:(UIImageView *)imageView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [imageView setCenter:CGPointMake(-80,355.5)];
    [UIView commitAnimations];
}

UIImageView *finger = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"finger.png"]];

// position finger off the screen
[finger setFrame:CGRectMake(142.5,480,152.5,243)];
[self.view addSubview:finger];

[self performSelector:@selector(fingerSwipeUp:) withObject:finger afterDelay:6];
[self performSelector:@selector(fingerSwipeLeft:) withObject:finger afterDelay:8];

任何帮助非常感谢

2 个答案:

答案 0 :(得分:1)

我猜你在ViewDidAppear()中创建了“finger”视图?如果是这种情况,每次翻页(隐藏)然后返回时,您都会添加另一个箭头(子视图)。那又怎么样呢?

if (finger == nil) {
    finger = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"finger.png"]];
    [finger setFrame:CGRectMake(142.5,480,152.5,243)];
    [self.view addSubview:finger];
}

[self performSelector:@selector(fingerSwipeUp:) withObject:finger afterDelay:6];
[self performSelector:@selector(fingerSwipeLeft:) withObject:finger afterDelay:8];

答案 1 :(得分:0)

如果序列中有许多动画,您可能会发现使用基于块的UIView动画语法更容易:

    [UIView animateWithDuration:1.0 delay:6.0 options:UIViewAnimationCurveEaseInOut animations:^{

    // animation 1

} completion:^(BOOL finished) {

        [UIView animateWithDuration:1.0 delay:8.0 options:UIViewAnimationCurveEaseInOut animations:^{

        // animation 2
    }];

}];

这只会在动画1完成后启动动画2 ,因此您不必担心在延迟动画2时允许动画1的持续时间。