Meg*_*anX 3 objective-c rotation uibutton ios
我有UIButton,我希望在一个方向旋转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;
}];
}
}
Run Code Online (Sandbox Code Playgroud)
它第一次工作,但第二次按下按钮,没有任何反应.有人可以解释我在这里做错了什么吗?
rob*_*off 15
实现此效果的最简单方法是旋转小于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;
}
}];
}
Run Code Online (Sandbox Code Playgroud)
结果:
Ibr*_*mar 11
这是@rob mayoff解决方案的Swift3代码.
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
}
})
Run Code Online (Sandbox Code Playgroud)
Swift4
M_PI 已弃用并替换为 Double.pi
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
}
})
Run Code Online (Sandbox Code Playgroud)
M_PI并且-M_PI将具有相同的视觉效果
转回原来的位置应该是
self.blurButton.transform = CGAffineTransformMakeRotation(0);
Run Code Online (Sandbox Code Playgroud)
- 编辑 -
要为逆时针旋转设置动画,您可以使用 -2*M_PI
self.blurButton.transform = CGAffineTransformMakeRotation(-2*M_PI);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6584 次 |
| 最近记录: |