Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated CGAffineTransformTranslate after CGAffineTransformScale results in "jumping" frame

Consider a simple Xcode 6 Swift "Single View" project. AutoLayout is deactivated.

The view (all Autoresizing is deactivated) contains one 100x100 UIImageView (image) and two buttons "Move" and "Resize", triggering the respective functions in the ViewController:

//
//  ViewController.swift
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var image: UIImageView!

    @IBAction func resize(sender: UIButton) {
        UIView.animateWithDuration(1.0) {
            self.image.transform = CGAffineTransformScale(self.image.transform, 2, 2)
        }
    }

    @IBAction func move(sender: AnyObject) {
        UIView.animateWithDuration(1.0) {
            self.image.transform = CGAffineTransformTranslate(self.image.transform, 10, 0);
        }
    }
}

Button Move shall move the image by 10 points to the right. Button Resize shall scale the image by 2 in each dimension. Each transformation shall be animated with a duration of 1 second.

Each button for itself works perfectly fine. Problems however arise as soon as the buttons are clicked sequentially, e.g. first Move (I wait for the animation to complete) and then Resize (or the other way around). In this case, the second animation does not originate where it is supposed to. Instead, the image first "jumps" several points into the wrong direction and then resumes the intended animation to the (correct) new position.

My intention is to trigger multiple animated transitions individually after each other, so this "jump" effect is a major annoyance. Thanks a lot for any ideas on how to solve the issue. Please let me know in case I can supply further details.

Edit: Please let me highlight again that all AutoLayout and Autoresizing is deactivated and hence should not cause the issue.

like image 992
Joshua Avatar asked Dec 11 '25 21:12

Joshua


1 Answers

Instead of using CGAffineTransformScale, change the frame to achieve the same effect.

Move the view:

UIView.animateWithDuration(1.0, delay: 0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
        self.theView.transform = CGAffineTransformTranslate(self.theView.transform, 0, 20)
        }, completion:nil)

Scale the view:

UIView.animateWithDuration(1.0) {
        self.theView.frame = CGRectInset(self.theView.frame, 0.05 * self.theView.frame.width, 0.05 * self.theView.frame.height)
    }
like image 198
gabbler Avatar answered Dec 14 '25 11:12

gabbler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!