我对iOS中的动画工具了如指掌。 旋转,缩放和移动,但我做了一个糟糕的时机
例如,我左边有两个正方形,右边有另一个正方形 我希望两个方格两个旋转,直到它们与角度相互碰撞 但由于时机不好,我没能成功所以我的问题是他们的神教程说明了
答案 0 :(得分:0)
也许您可以查看this链接。正如你的标签暗示你正在使用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];
}
更新2: 我改变了旋转的持续时间和速度。现在它看起来像你的示例动画。两个方块现在彼此适合。
更新3: 如你所说,你有时间问题,以下指南帮助我:
答案 1 :(得分:0)
您是否正在实施UICollisionBehavior?如果是这样,请尝试禁用它。