顺时针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
}
}
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)
}
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];
享受吧!
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度给出一个逆时针方向。
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)