【问题标题】:Adding new animation to the existing one向现有动画添加新动画
【发布时间】:2012-07-10 15:25:24
【问题描述】:

我的应用程序我想将 UIImageView 从默认位置移动到用户点击的位置。但是,如果在完成此动画之前,用户再次点击其他点,则该 imageView 不应从原始位置开始,而是应从点击新点后停止动画的点开始。我怎样才能做到这一点。以下是将 imageView 移动到用户点击位置的代码。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


UITouch *touch=[[event allTouches]anyObject];
touchedPoint= [touch locationInView:touch.view];

[imageViews.layer removeAllAnimations];

[UIImageView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    [CATransaction begin];        
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.duration = 2.0f;
    pathAnimation.calculationMode = kCAAnimationPaced;
    animationPath = [UIBezierPath bezierPath];
    [animationPath moveToPoint:imageViews.center];
    [animationPath addLineToPoint:CGPointMake(touchedPoint.x,touchedPoint.y)];

    pathAnimation.path = animationPath.CGPath;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    [imageViews.layer addAnimation:pathAnimation forKey:@"animatePosition"];
    [CATransaction commit];


}
completion:nil]; //or completion:^(BOOL) finished { your code here for when the animation ended}];

}

【问题讨论】:

    标签: objective-c ios cocoa-touch ipad cakeyframeanimation


    【解决方案1】:

    改用这个:

    [UIImageView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    } completion:nil];
    

    UIViewAnimationOptionBeginFromCurrentState 选项将使动画从图像的当前位置开始。因此,如果您将图像与另一个动画一起移动,它会很好地融合在一起。

    希望这会有所帮助。

    干杯!

    【讨论】:

    • 谢谢,但问题还是一样。
    • 我应该删除 [imageview.layer removeallanimation] 吗? movetopoint 的值应该是多少?
    • 是的,删除[imageview.layer removeallanimation]。 moveToPoint 有你想要的任何参数。它的价值因您要完成的工作而异。
    • 我删除了那条线,但动画从添加 imageView 的原始点开始。
    【解决方案2】:
    1. 定义一个全局 CGPoint;
    2. 沿动画启动一个 0.1 秒的步长重复计时器(在用户第一次点击时)。
    3. 在每次滴答计数时,读取图像的位置并设置上面定义的 CGPoint。
    4. 动画开始后,当用户再次点击时,您会停止上一个动画,并启动另一个动画,使用上面的 CGPoint 作为“moveToPoint”。

    如果动画在用户点击改变其方向之前停止,则计时器无效。

    【讨论】:

    • 我按照你说的添加了计时器,但在计时器方法中,我将图像 view.center 作为原始中心。 Imageview 没有更新值。
    • 你是对的。在这种情况下,您必须在计时器中管理所有动画。看,或者,您有一个起点和一个终点(第一个动画)。你找到运动的速度,启动计时器,计算两点之间的距离,使用Dist = Speed/Time,你可以找到坐标,这样你就得到了运动物体的框架。
    猜你喜欢
    • 1970-01-01
    • 2018-04-16
    • 2019-07-26
    • 1970-01-01
    • 2011-04-13
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多