iOS 上的动画方向

Animation Direction on iOS

我对 iOS 中的动画工具了如指掌。 旋转、缩放和移动,但我选错了时机

例如我有两个正方形,一个在左边,另一个在右边 我希望这两个正方形旋转两次,直到它们以 Angle 相互碰撞 但我没能做到,因为时机不对

所以我的问题是他们的神教程说明它

也许你可以看看 this link。由于您的标签建议您为此使用 CoreAnimation,因此您可以链接您的动画。

您可以有一个固定的水平移动和一个具有相同旋转持续时间的嵌套动画。我就是这样完成这些任务的。

更新: 这是一些非常适合我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 70.0, 30.0, 30.0)];
    leftView.backgroundColor = [UIColor redColor];
    [self.view addSubview:leftView];

    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 50.0, 50.0, 50.0)];
    rightView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:rightView];

    [self moveViewLeft:leftView];
    [self moveViewRight:rightView];

}

- (void)moveViewLeft:(UIView *)view {

    float duration = 1.0;

    // Moving
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue=[NSValue valueWithCGPoint:CGPointMake([view center].x, [view center].y)];
    moveAnimation.toValue=[NSValue valueWithCGPoint:CGPointMake([view center].x+self.view.frame.size.width/2-view.frame.size.width, [view center].y)];
    moveAnimation.duration = duration;
    moveAnimation.speed = 1.0;
    moveAnimation.fillMode = kCAFillModeForwards;
    moveAnimation.removedOnCompletion = NO;

    // Rotation
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: ((360*M_PI)/180)];
    rotationAnimation.duration = duration+0.5;
    rotationAnimation.speed = 1.5;
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.removedOnCompletion = NO;

    // Combining Animations

    CAAnimationGroup *anim = [CAAnimationGroup animation];
    anim.animations = [NSArray arrayWithObjects:moveAnimation, rotationAnimation, nil];
    anim.duration = duration;
    anim.fillMode = kCAFillModeForwards;
    anim.removedOnCompletion = NO;

    [view.layer addAnimation:anim forKey:nil];

}

- (void)moveViewRight:(UIView *)view {

    float duration = 1.0;

    // Moving
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue=[NSValue valueWithCGPoint:CGPointMake([view center].x, [view center].y)];
    moveAnimation.toValue=[NSValue valueWithCGPoint:CGPointMake([view center].x-self.view.frame.size.width/2, [view center].y)];
    moveAnimation.duration = duration;
    moveAnimation.speed = 1.0;
    moveAnimation.fillMode = kCAFillModeForwards;
    moveAnimation.removedOnCompletion = NO;

    // Rotation
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: ((360*M_PI)/180)];
    rotationAnimation.duration = duration+5;
    rotationAnimation.speed = 1.5;
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.removedOnCompletion = NO;

    // Combining Animations

    CAAnimationGroup *anim = [CAAnimationGroup animation];
    anim.animations = [NSArray arrayWithObjects:moveAnimation, rotationAnimation, nil];
    anim.duration = duration;
    anim.fillMode = kCAFillModeForwards;
    anim.removedOnCompletion = NO;

    [view.layer addAnimation:anim forKey:nil];

}

更新二: 我改变了旋转的持续时间和速度。现在它看起来像你的示例动画。两个正方形现在彼此适合。

更新 3: 正如你所说,你有时间问题,以下指南帮助了我:

  • 旋转的持续时间应等于速度。
  • 持续时间和速度应为移动动画持续时间和速度的 150%。

你在实现 UICollisionBehavior 吗?如果是这样,请尝试禁用它。