Ios 将按钮旋转180度并将其旋转回来
我有一个Ios 将按钮旋转180度并将其旋转回来,ios,objective-c,uibutton,rotation,Ios,Objective C,Uibutton,Rotation,我有一个ui按钮,我希望在一个方向上旋转180度,然后再旋转180度。我已经用CABasicAnimation做了一段时间的动画,但这次我想用转换来做。以下是我编写的代码: - (IBAction)blurAction:(id)sender { if (self.blurActive) { [UIView animateWithDuration:0.1 animations:^{ self.blurView.alpha = 0;
ui按钮
,我希望在一个方向上旋转180度,然后再旋转180度。我已经用CABasicAnimation
做了一段时间的动画,但这次我想用转换来做。以下是我编写的代码:
- (IBAction)blurAction:(id)sender {
if (self.blurActive) {
[UIView animateWithDuration:0.1 animations:^{
self.blurView.alpha = 0;
self.blurButton.transform = CGAffineTransformMakeRotation(M_PI);
} completion:^(BOOL finished) {
self.blurActive = NO;
}];
}
else {
[UIView animateWithDuration:0.1 animations:^{
self.blurView.alpha = 1;
self.blurButton.transform = CGAffineTransformMakeRotation(-M_PI);
} completion:^(BOOL finished) {
self.blurActive = YES;
}];
}
}
它第一次工作,但第二次我按下按钮,什么也没发生。有人能解释一下我做错了什么吗?
mupi
和-mupi
将具有相同的视觉效果
回到原来的位置应该是
self.blurButton.transform = CGAffineTransformMakeRotation(0);
--编辑--
要设置逆时针旋转的动画,可以使用-2*M\u PI
self.blurButton.transform = CGAffineTransformMakeRotation(-2*M_PI);
若要“取消旋转”,请将视图的变换设置为常量CGAffineTransformity,该值为零。这就是使用变换的美妙之处,您不需要计算返回的路径,只需撤消您已经执行的操作 实现这种效果最简单的方法就是旋转一个非常小的小于180°的角度:
- (IBAction)toggle:(UIButton *)sender {
[UIView animateWithDuration:0.2 animations:^{
if (CGAffineTransformEqualToTransform(sender.transform, CGAffineTransformIdentity)) {
sender.transform = CGAffineTransformMakeRotation(M_PI * 0.999);
} else {
sender.transform = CGAffineTransformIdentity;
}
}];
}
结果:
这是@rob mayoff解决方案的Swift3代码
@IBAction func fooButton(_ sender: Any) {
UIView.animate(withDuration:0.1, animations: { () -> Void in
if sender.transform == .identity {
sender.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 0.999))
} else {
sender.transform = .identity
}
})
}
Swift4
M_PI
已被弃用并替换为Double.PI
@IBAction func fooButton(_ sender: Any) {
UIView.animate(withDuration:0.1, animations: { () -> Void in
if sender.transform == .identity {
sender.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * 0.999))
} else {
sender.transform = .identity
}
})
}
使用转换实际上无法实现这一点,因为转换只保存有关结束状态的信息。它们没有告诉我们应该使用哪个方向进行旋转,例如旋转 实现180度前后旋转的最佳选择是使用
CABasicAnimation
下面是一个例子:
func rotateImage(forward: Bool) {
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.fromValue = forward ? 0.0 : CGFloat.pi
rotationAnimation.toValue = forward ? CGFloat.pi : 0.0
rotationAnimation.fillMode = kCAFillModeForwards
rotationAnimation.isRemovedOnCompletion = false
rotationAnimation.duration = 0.5
handleImageView.layer.add(rotationAnimation, forKey: "rotationAnimation")
}
将按钮旋转180º
UIButton.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
将按钮旋转0º或再次旋转
UIButton.transform = CGAffineTransform(rotationAngle: 0)
打开动画的示例
if cardViewModel.getBottomMenuVisibility() {
[UIView .transition(
with: bottomMenuView,
duration: 0.3,
options: .transitionCrossDissolve,
animations: {
// Turn UIButton upside down
self.bottomMenu.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
},
completion: nil)]
}
else {
[UIView .transition(
with: bottomMenuView,
duration: 0.3,
options: .transitionCrossDissolve,
animations: {
// Turn UIButton back to normal
self.bottomMenu.transform = CGAffineTransform(rotationAngle: 0)
},
completion: nil)]
}
您可以在下面的示例中查看我的代码:
Swift 4+
@IBAction private func maximizeButtonAction(_ sender: UIButton) {
UIView.animate(withDuration: 0.2, animations: {
sender.transform = sender.transform == .identity ? CGAffineTransform(rotationAngle: CGFloat(Double.pi * 0.999)) : .identity
})
}
不是真的,如果我把0,它会在同一个方向上再旋转180度,这将是原始位置,但它就像是顺时针旋转按钮360度。另一方面,我想顺时针旋转180度,然后逆时针旋转180度。对于逆时针旋转,你可以使用
-2*M_PI
不确定,只是在这里测试了一下,效果很好。上一次我做得不好,我没有提交代码。把这个答案放在评论里,这样我就可以接受了。UIButton仍然在同一个方向上旋转,所以它像是在同一个方向上旋转180度。我喜欢你的方法。以下是Swift 3代码:UIView.animate(持续时间:0.5,动画:{if self.transform==cgfaffinetransform.identity{self.transform=cgfaffinetransform(rotationAngle:(180.0*.pi)/180.0)}否则{self.transform=cgfaffinetransform.identity})