我是iOS的新手。
我想在用户点击按钮时始终旋转一半。
以下是代码,它只旋转一次,当我点击它时再也不会再旋转。
@property UIButton *button;
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat buttonSize = 220;
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.backgroundColor = [UIColor colorFromHexString:@"#B084C7"];
self.button.frame = CGRectMake(0, 0, buttonSize, buttonSize);
[self.button addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)didTap:(id)sender {
POPSpringAnimation *anim = [self.button.layer pop_animationForKey:@"rotate"];
if (anim) {
anim.toValue = @(M_PI * 0.5);
} else {
anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerRotation];
anim.toValue = @(M_PI * 0.5);
anim.springSpeed = 3;
anim.springBounciness = 1;
}
[self.button.layer pop_addAnimation:anim forKey:@"rotate"];
}
答案 0 :(得分:1)
您始终旋转到相同的值@(M_PI * 0.5)
这是应用于图层的绝对旋转。如果你想增加角度,那么你需要做这样的事情:
anim.toValue = @(anim.fromValue.floatValue + (M_PI * 0.5));