swift2 使按钮顺时针旋转360度并反向旋转

tjvv9vkg  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(157)

顺时针360度旋转一个按钮,然后逆时针360度旋转。但是没有动画...动画总是顺时针360度旋转。

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

        }
    }
oxcyiej7

oxcyiej71#

Swift 5上的简单通用解决方案

你可以旋转任何东西

//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)
}
tjrkku2a

tjrkku2a2#

Objective-C中的PFB答案:

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];

好好享受吧!

5w9g7ksd

5w9g7ksd3#

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)

这是我假设:
旋转是在最短的方向上完成的。由于180度在两种方向上都是相同的,所以它总是顺时针旋转。所以我们用90度到逆时针来给予一个方向。

ruarlubt

ruarlubt4#

雨燕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)

雨燕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)

相关问题