Animated CGAffineTransformTranslate after CGAffineTransformScale results in frame jumping

Consider a simple Xcode 6 Swift "Single View" project. Autostart disabled.

The view (all Autoresizing deactivates) contains one 100x100 UIImageView ( image ) and two Move and Resize buttons, calling the corresponding 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);
        }
    }
}

      

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

Each button works great for itself. However, problems arise as soon as the buttons are pressed in sequence, for example. Move first (I'm waiting for the animation to complete) and then Resize (or vice versa). In this case, the second animation does not appear where it should. Instead, the image first "jumps" a few points in the wrong direction and then resumes the intended animation to the (correct) new position.

My intention is to initiate multiple animated transitions separately one after the other, so this "jump" effect is a major annoyance. Thanks so much for any ideas on how to resolve the issue. Please let me know if I can provide more information.

Change: . Let me emphasize once again that all autoruns and autoresizations are deactivated and therefore should not cause problems.

+3


source to share


1 answer


CGAffineTransformScale

Change the frame instead to achieve the same effect.

Move 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)
    }

      

+1


source







All Articles