所以,我已经工作了几个小时试图解决这个问题,我认为可能只是缺少一些如此明显的东西。
我想做的是有一条线的动画(就像时钟秒针)顺时针旋转,但是当屏幕被按下时,它会停止,但是当再次按下时它再次启动但是旋转反转并且线开始逆时针旋转...
这是我用来围绕固定点旋转线的代码,它可以工作:
- (void) spinWithOptions: (UIViewAnimationOptions) options {
[UIView animateWithDuration: 0.0f
delay: 0.0f
options: options
animations: ^{
Hand.layer.anchorPoint = CGPointMake(0.5,1);
Hand.transform = CGAffineTransformRotate(Hand.transform, M_PI / 40);
}
completion: ^(BOOL finished) {
if (finished) {
if (animating) {
// if flag still set, keep spinning with constant speed
[self spinWithOptions: UIViewAnimationOptionCurveLinear];
} else if (options != UIViewAnimationOptionCurveEaseOut) {
// one last spin, with deceleration
//[self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
}
}
}];
}
- (void) startSpin {
if (!animating) {
animating = YES;
[self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
Clockwise = NO;
}
}
- (void) spinWithOptions_Anti: (UIViewAnimationOptions) options {
[UIView animateWithDuration: 0.0f
delay: 0.0f
options: options
animations: ^{
Hand.layer.anchorPoint = CGPointMake(0.5,1);
Hand.transform = CGAffineTransformRotate(Hand.transform, -M_PI / 40);
}
completion: ^(BOOL finished) {
if (finished) {
if (animating) {
// if flag still set, keep spinning with constant speed
[self spinWithOptions: UIViewAnimationOptionCurveLinear];
} else if (options != UIViewAnimationOptionCurveEaseOut) {
// one last spin, with deceleration
//[self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
}
}
}];
}
- (void) startSpin_Anti {
if (!animating) {
animating = YES;
[self spinWithOptions_Anti: UIViewAnimationOptionCurveEaseIn];
Clockwise = YES;
}
}
- (void) stopSpin {
animating = NO;
}
因此,在应用程序开始时,当 ViewDidload 调用startSpin
时,该行开始顺时针旋转,调用spinWithOptions
...
果然,如果我按下屏幕,旋转动画就会停止,这就是......
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
NSLog(@"Dircetion: %i", Clockwise);
if(animating){
[self stopSpin];
}
else{
if(Clockwise){
[self startSpin];
NSLog(@"must be clockwise");
}
else{
[self startSpin_Anti];
NSLog(@"anti");
}
}
}
但是当再次按下时它会开始旋转,但它永远不会改变方向......它只能顺时针旋转......
我似乎无法理解为什么它不会改变方向......甚至NSLogs都说顺时针方向的布尔是正确的......