Bram'in Bram'in - 5 months ago 29
Swift Question

Repeat a CGAffineTransformMakeRotation in Swift

I want to replace a view with a kind of fadeIn/fadeOut with rotation.
Here is my function

func animateSendButton(enter: Bool)
{
let rotation: CGFloat = enter ? 180 : -180

UIView.animateWithDuration(0.5, animations: {
let transform = CGAffineTransformMakeRotation(self.DEGREES_TO_RADIANS(rotation))
self.sendImage.transform = transform
self.sendImage.layer.opacity = enter ? 0.0 : 0.87
self.sendImage2.transform = transform
self.sendImage2.layer.opacity = enter ? 0.87 : 0.0

})
}


However the rotation works just one time and after only opacity change.
I tried with CGATransformRotate but it didn't worked.

Is there a way to repeat it ?

Answer

Try resetting the transform to its identity :)

func animateSendButton(enter: Bool)
{
    let alpha = enter ? 0.0 : 0.87
    let transfrom = enter ? CGAffineTransformRotate(CGAffineTransformIdentity, DEGREES_TO_RADIANS(180)) : CGAffineTransformIdentity

    UIView.animateWithDuration(0.6, delay: 0.0, options: .CurveEaseIn, animations: {
        self.sendImage.transform = transform
        self.sendImage.layer.opacity = alpha
        self.sendImage2.transform = transform
        self.sendImage2.layer.opacity = alpha

    }) { (complete) in

    }
}
Comments