动画按钮360顺时针和反向

时间:2016-03-24 05:02:04

标签: ios swift uibutton swift2 uiviewanimation

顺时针旋转一个按钮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

        }
    }

4 个答案:

答案 0 :(得分:6)

Swift

上的简单常见解决方案

你可以旋转任何东西

//Clock-wise
rotateAnyView(makeOrderButton, fromValue: 0, toValue: 2.0 * M_PI, duration: 1)

//Reverse
rotateAnyView(makeOrderButton, fromValue: 2.0 * M_PI, toValue:0, duration: 1)

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

答案 1 :(得分:1)

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

享受!

答案 2 :(得分:0)

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度逆时针方向给出方向。

答案 3 :(得分:0)

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        "http://db-1:1234",
        "http://db-2:1234",
        "http://db-3:1234"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        "http://db-server-1:1234",
        "http://db-srv-2:1234",
        "http://server-3:1234"
    ]
}