Will Chen
Will Chen

Reputation: 74

How to animate UIView transform sequence like scaling then rotating in iOS?

I wonder how to make a UIView animation that first scale to size and rotate that size instead of rotating on the original size?

I've tried a lot of ways like wrapping UIView animation in the completion of a UIView animation, using UIView animateKeyframes and so on.

However, I can't build a animation like it perfectly, please give me hints or keywords to search, thanks!

Upvotes: 4

Views: 5620

Answers (3)

Ketan Parmar
Ketan Parmar

Reputation: 27428

You can do something like,

Define radians to convert degrees into radians

#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)

then in your viewDidAppear,

-(void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

CGAffineTransform leftTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));

_myView.transform = leftTransform;  //Here `_myView` is the your view on which you want animations!

[UIView beginAnimations:@"youCanSetAnimationIdHere" context:(__bridge void * _Nullable)(_myView)];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:5];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationCompletedWithAnimationId:isCompleted:view:)];

_myView.transform = rightTransform;

[UIView commitAnimations];

}

and your animationCompletedWithAnimationId method should be like,

- (void) animationCompletedWithAnimationId:(NSString *)animationID isCompleted:(NSNumber *)isCompleted view:(UIView *)view
{
if ([isCompleted boolValue]) {
    UIView *myView = view;
    myView.transform = CGAffineTransformIdentity;

}
}

And the result is

enter image description here

You can change repeat count and radians as per your requirement !

Upvotes: 2

Krunal
Krunal

Reputation: 79636

This should work according to your requirement (how to make a UIView animation that first scale to size and rotate that size instead of rotating on the original size?)

@IBOutlet var scaleRotateImage: UIImageView!
func scaleNTransform() -> Void {

    scaleRotateImage.layer.cornerRadius = scaleRotateImage.frame.size.height/2.0
    scaleRotateImage.clipsToBounds = true

    let scaleTransform = CGAffineTransform(scaleX: 3.0, y: 3.0)  // Scale
    let rotateTransform = CGAffineTransform(rotationAngle: CGFloat.pi) // Rotation
    let hybridTransform = scaleTransform.concatenating(rotateTransform) // Both Scale + Rotation

    // Outer block of animation
    UIView.animate(withDuration: 5.0, animations: {
        self.scaleRotateImage.transform = scaleTransform
        self.view.layoutIfNeeded()
    }) { (isCompleted) in

        // Nested block of animation
        UIView.animate(withDuration: 5.0, animations: {
            self.scaleRotateImage.transform = hybridTransform
            self.view.layoutIfNeeded()
        })

    }


}


Result

enter image description here

Upvotes: 5

Baig
Baig

Reputation: 4995

Hoping you have achieved scaling or shaking your UIButton, this code will guide you to make UIView animation sequence repeat and autoreverse.

btn.frame = frame1;
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
        // Your changes to the UI, eg. scale UIButton up 300 points
        btn.frame = frame2;
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
        // Your changes to the UI, eg. rotate or shake UIButton
    }];
} completion:nil];

Upvotes: 0

Related Questions