Objective-C:块中的动画
Objective-C: Animations in Blocks
我有一个 UIImageView sliderPump
,我通过一个接一个地调用两个方法从屏幕的右侧移动到左侧无限次:
-(void)pumpGoesRight
{
if (slide)
{
[UIView animateWithDuration:0.8 animations:^
{
[sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x+235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)];
} completion:^(BOOL finished)
{
[self pumpGoesLeft];
}];
}
else
return;
}
-(void)pumpGoesLeft
{
if (slide)
{
[UIView animateWithDuration:0.8 animations:^
{
[sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x-235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)];
} completion:^(BOOL finished)
{
[self pumpGoesRight];
}];
[self performSelector:@selector(pumpGoesRight) withObject:nil afterDelay:0.8];
}
else
return;
}
然后我使用这段代码找出 UIImageView X 的位置 sliderPump
并停止动画:
-(void)stop
{
slide = NO;
NSLog(@"Slide Pump position: %f", sliderPump.layer.frame.origin.x);
[self.view.layer removeAllAnimations];
}
问题是当我调用 stop 方法时,我想得到 sliderPump
的当前位置,但我得到的是 sliderPimp
在结束时的最终位置动画,即使动画尚未完成。我怎样才能得到框架的当前位置?谢谢!
你应该使用表现层。
NSLog(@"Slide Pump position: %f", [sliderPump.layer.presentationLayer frame].origin.x);
我有一个 UIImageView sliderPump
,我通过一个接一个地调用两个方法从屏幕的右侧移动到左侧无限次:
-(void)pumpGoesRight
{
if (slide)
{
[UIView animateWithDuration:0.8 animations:^
{
[sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x+235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)];
} completion:^(BOOL finished)
{
[self pumpGoesLeft];
}];
}
else
return;
}
-(void)pumpGoesLeft
{
if (slide)
{
[UIView animateWithDuration:0.8 animations:^
{
[sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x-235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)];
} completion:^(BOOL finished)
{
[self pumpGoesRight];
}];
[self performSelector:@selector(pumpGoesRight) withObject:nil afterDelay:0.8];
}
else
return;
}
然后我使用这段代码找出 UIImageView X 的位置 sliderPump
并停止动画:
-(void)stop
{
slide = NO;
NSLog(@"Slide Pump position: %f", sliderPump.layer.frame.origin.x);
[self.view.layer removeAllAnimations];
}
问题是当我调用 stop 方法时,我想得到 sliderPump
的当前位置,但我得到的是 sliderPimp
在结束时的最终位置动画,即使动画尚未完成。我怎样才能得到框架的当前位置?谢谢!
你应该使用表现层。
NSLog(@"Slide Pump position: %f", [sliderPump.layer.presentationLayer frame].origin.x);