It should be something really simple, but I was not able to achieve this using blocks. There are questions and answers to this, but everything that I found is solved using CABasicAnimation , and not UIView Block-Based Animation, which I do after.
The following code does not work (Block-Based), without animation:
CGAffineTransform spin = CGAffineTransformRotate(spiningView.transform, DEGREES_RADIANS(360)); CATransform3D identity = CATransform3DIdentity; CATransform3D spin2 = CATransform3DRotate(identity, DEGREES_RADIANS(360), 0.0f, 0.0f, 1.0f); [UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^ { spiningView.transform = spin;
From my understanding, every time we use Block-Based, the animation will not occur when the UIViewAnimation Block "sees" that the initial value matches the final value. Fairly enough, to move it 360 degrees would mean that the object remains where it is. But this should be a way to use Block-Based Animation to make this animated, because the following CABasicAnimation will work flawlessly:
CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0f]; rotationAnimation.duration = 3.0f; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 1; [spiningView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
In addition to this, the following block-based actions are performed, but there is a stop between the animations (first it rotates 180 degrees, and then 180 degrees to complete the rotation), and this is not what I get after:
[UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^ { spiningView.transform = CGAffineTransformRotate(spiningView.transform, DEGREES_RADIANS(180));; } completion:^(BOOL finished) { [UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^ { spiningView.transform = CGAffineTransformRotate(spiningView.transform, DEGREES_RADIANS(360)); } completion:^(BOOL finished) { }]; }];
I know that I could save a lot of time and just CABasicAnimation and use CABasicAnimation with it, but I would like to know why it works and the other one does not work this case (360 degree rotation). I hope you can give me a detailed explanation regarding this between 2 in this case and some Block-Based code that can perform a full rotation of 360 degrees.
Thanks in advance.
ios core-graphics uiviewanimation cabasicanimation
Unheilig
source share