Use two different UIViewAnimationCurves in one animation

I want to use one UIViewAnimationCurve to rotate and the other to change position. Is it possible?

For example (in pseudo-code);

// Begin animations

// Set rotation animation curve to EaseInOut

// Set position animation curve to Linear

// Make some changes to position

// Make some change to the angle of rotation

// Commit the animations

EDIT: (CAAnimationGroup approach suggested below) - created 2 separate CABasicAnimations and CAAnimationGroup, however animations do not start. Any ideas?

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToWorldSpaceFromViewSpace:self.spriteView.position]];
postionAnimation.delegate = self;

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.z"];
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.angle -= M_PI_2];
rotationAnimation.delegate = self;

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation];
animationsGroup.duration = self.clockSpeed;
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil];

// Perform the animation
[self.spriteView.layer addAnimation:animationsGroup forKey:nil];
+5
source share
3 answers

It is best to use multiple calls for animateWithDuration: delay: options: animations: completion :. Use different animation curves in each call, and they will work in parallel. Or, with different delay values, you can make them work in sequence.

The code might look like this:

[UIView animateWithDuration: .5 
  delay: 0
  options: UIViewAnimationOptionCurveEaseInOut 
  animations: ^{
    view1.center = CGPointMake(x, y);
  }
  completion:  ^{
    //code that runs when this animation finishes
  }
];

[UIView animateWithDuration: .5 
  delay: .5
  options: UIViewAnimationOptionCurveLinear
  animations: ^{
    view2.center = CGPointMake(x2, y2);
  }
  completion:  ^{
    //code that runs when this animation finishes
  }
];

CAAnimationGroups, .

. , (, , ).

-, , . .

+3

. UIView . CAAnimations CAAnimationGroup, CABasicAnimation

+4

, . iSofTom ( ). , .

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
postionAnimation.fromValue = [NSValue valueWithCGPoint:self.spriteView.center];
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]];

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.fromValue = [NSNumber numberWithFloat:self.spriteView.angle];
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.rotation];

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation];
animationsGroup.duration = self.clockSpeed;
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil];
animationsGroup.delegate = self;
// Perform the animation
[self.spriteView.layer addAnimation:animationsGroup forKey:nil];

- (void)animationDidStart:(CAAnimation *)theAnimation {
radiansToDegrees(self.spriteView.rotation));
    self.spriteView.angle = self.spriteView.rotation;

NSStringFromCGPoint(self.spriteView.position));
    self.spriteView.center = [self transformPointToViewSpaceFromWorldSpace:self.spriteView.position];
}
0

All Articles