Dliix
Dliix

Reputation: 716

Swift Animation on flipped Lable

I have a problem in Swift 2 that I can't fix...

I have a UILabel and a UIView that are both upside down. I added an extension to UIView to perform an animation (grow then shrink) that is performed when I click on a button. But when the animations are performed, the UILabel turns back by 180° during the animation whereas the UIView does not... Any idea about why is this happening ?

Here is the code:

UIView's extension:

func animateBounce(multiplier: CGFloat, duration: NSTimeInterval)
{
    UIView.animateWithDuration(duration / 2, animations:
    {
        self.transform = CGAffineTransformMakeScale(multiplier, multiplier)
    }, completion:
    {
        finish in
        UIView.animateWithDuration(duration / 2)
        {
            self.transform = CGAffineTransformIdentity
        }
    })
}

How I flip the UIView and UILabel:

label.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))

How I call the animation:

let multiplier = 1.5 as CGFloat
let animationTime = 0.5 as NStimeInterval

label.animateBounce(1.5, duration: animationTime)
view.animateBounce(1.5, duration: animationTime)

ANSWER: (credits to matt) I was overwriting the transform property of the label, thus removing the previous transformation (the rotation). You should use CGAffineTransformConcat to add add a new transform without removing the previous ones.

Upvotes: 0

Views: 1076

Answers (2)

steffi pravasi
steffi pravasi

Reputation: 101

flip a label when the data changes swift 4.

I have flipped a label from top to bottom where my label is declared as:

@IBOutlet var cartCountLbl: UILabel! in "viewDidLoad()".

Call the function with this method

self.perform(#selector(self.flip), with: nil, afterDelay: 0) the flip method is declared as:

    @objc func flip() {
        let transitionOptions: UIViewAnimationOptions = [.transitionFlipFromTop, .showHideTransitionViews] 
        UIView.transition(with: cartCountLbl, duration: 1.0, options: transitionOptions, animations: {
        })
    }

Upvotes: 2

matt
matt

Reputation: 534950

You are saying

self.transform = CGAffineTransformMakeScale(multiplier, multiplier)

and

self.transform = CGAffineTransformIdentity

Those lines specify the whole transform that self will have. Thus, they remove all existing transforms, including the transform that originally turned the label upside down, replacing them with the transforms you specify.

If that isn't what you want to do, don't do that. For example, instead of calling CGAffineTransformMakeScale, call CGAffineTransformScale, which lets you start with the existing transform on your view and concatenate a scale transform to that.

Upvotes: 1

Related Questions