user4790024
user4790024

Reputation:

Animate button 360 clockwise and reverse back

Animate a button 360 degree clockwise and then reverse the animation to 360 counterclockwise.But doesnot animation...The animation is always 360 clockwise direction.

func animateShowingOfMenu(){

        let angle = CGFloat(180 * M_PI / 180)
        UIView.animateWithDuration(0.4
            , animations: { () -> Void in

                self.blurView.hidden=false
                self.menuBtn.transform = CGAffineTransformMakeRotation(angle)
                self.menuBtn.transform = CGAffineTransformMakeRotation(0)


            }) { (completion) -> Void in

                print("compeleted")
        }

    }

    func animateHidingOfMenu(){

        let angle = CGFloat(-180 * M_PI / 180)
        UIView.animateWithDuration(0.4
            , animations: { () -> Void in

                self.blurView.hidden = true
                self.menuBtn.transform = CGAffineTransformMakeRotation(angle)
                self.menuBtn.transform = CGAffineTransformMakeRotation(0)


            }) { (completion) -> Void in

        }
    }

Upvotes: 2

Views: 2081

Answers (4)

korgx9
korgx9

Reputation: 1264

Simple common solution on Swift 5

You can rotate anything

    //Clock-wise
    rotateAnyView(view: makeOrderButton, fromValue: 0, toValue: 2.0 * Double.pi, duration: 1)

    //Reverse
    rotateAnyView(view: makeOrderButton, fromValue: 2.0 * Double.pi, toValue:0, duration: 1)


}

func rotateAnyView(view: UIView, fromValue: Double, toValue: Double, duration: Double = 1) {
    let animation = CABasicAnimation(keyPath: "transform.rotation")
    animation.duration = duration
    animation.fromValue = fromValue
    animation.toValue = toValue
    view.layer.add(animation, forKey: nil)
}

Upvotes: 4

Amardeep Bhatia
Amardeep Bhatia

Reputation: 1

Swift 2

UIView.animateKeyframesWithDuration(1.0, delay: 0, options: [.Repeat], animations: {
    UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransformMakeRotation(90.0 * CGFloat(M_PI)/180.0)
    })
    UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransformMakeRotation(180.0 * CGFloat(M_PI)/180.0)
    })
    UIView.addKeyframeWithRelativeStartTime(0.50, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransformMakeRotation(270.0 * CGFloat(M_PI)/180.0)
    })
    UIView.addKeyframeWithRelativeStartTime(0.75, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransformMakeRotation(360.0 * CGFloat(M_PI)/180.0)
    })

}, completion: nil)

Swift 3, 4, 5

UIView.animateKeyframes(withDuration: 1.0, delay: 0, options: [.repeat], animations: {
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransform(rotationAngle: 90.0 * CGFloat.pi/180.0)
    })
    UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransform(rotationAngle: 180.0 * CGFloat.pi/180.0)
    })
    UIView.addKeyframe(withRelativeStartTime: 0.50, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransform(rotationAngle: 270.0 * CGFloat.pi/180.0)
    })
    UIView.addKeyframe(withRelativeStartTime: 0.75, relativeDuration: 0.25, animations: {
        self.imageV.transform = CGAffineTransform(rotationAngle: 360.0 * CGFloat.pi/180.0)
    })

}, completion: nil)

Upvotes: 0

Alok Subedi
Alok Subedi

Reputation: 1611

self.menuBtn.transform = CGAffineTransformMakeRotation(CGFloat(-90 * M_PI / 180))
self.menuBtn.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)
self.menuBtn.transform = CGAffineTransformMakeRotation(CGFloat(90 * M_PI / 180))
self.menuBtn.transform = CGAffineTransformMakeRotation(CGFloat(0)

this is my assumption:

The rotation is done in shortest direction. As 180 is same from both ways it always rotates clockwise. So we give a direction using 90 degrees to counterclockwise.

Upvotes: 0

atulkhatri
atulkhatri

Reputation: 11343

PFB answer in Objective-C:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 1;
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
animation.autoreverses = YES ;
[self.containerView.layer addAnimation:animation forKey:nil];

Enjoy!

Upvotes: 1

Related Questions