【发布时间】:2012-02-01 22:42:31
【问题描述】:
如何暂停和恢复 UIView 动画? (没有块动画)
我很难弄清楚这一点,所以下面是我关于如何做到这一点的来源。
【问题讨论】:
标签: cocoa-touch uiview uiviewanimation
如何暂停和恢复 UIView 动画? (没有块动画)
我很难弄清楚这一点,所以下面是我关于如何做到这一点的来源。
【问题讨论】:
标签: cocoa-touch uiview uiviewanimation
如何暂停和恢复 UIView 动画:
这将介绍如何在没有块动画的情况下执行上述操作。 (人们仍然希望支持 3.0 及更高版本)。
这仅允许在动画到达设置位置后暂停。对于如何在动画中间暂停动画,我建议使用 CALayers:
CALayer* myLayer = [self.myUIView.layer presentationLayer];
CGRect frameStop = myLayer.frame;
double pausedX = frameStop.origin.x;
double pausedY = frameStop.origin.y;
现在是实际如何:
startAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect];
startAnimation.titleLabel.font = [UIFont systemFontOfSize:22];
startAnimation.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation;
[startAnimation setTitle:(@"pause") forState:UIControlStateNormal];
[startAnimation addTarget:self action:@selector(doAnimation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startAnimation];
-(void)doAnimation{
if (bicyclePad.animationPause == false){
[restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal];
[bicyclePad pauseAnimation];
} else {
[restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal];
[bicyclePad resumeAnimation];
}
}
-(void)resumeAnimation{
bicyclePad.animationPause = false;
[restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal];
objectMotion = [[yourUIView alloc]initWithFrame:CGRectMake((*yourPathPoints)[0].x, (*yourPathPoints)[0].y, 8, 8)];
[self.view addSubview:objectMotion];
[self animateBike:nil finished:YES context:nil];
}
-(void)pauseAnimation{
bicyclePad.animationPause = true;
[restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal];
[bicyclePad doAnimation];
}
-(void)animateObject:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
//below suggesting your animation loops
if (animationPause == true)
[UIView setAnimationDidStopSelector:@selector(animateStop:finished:context:)];
else
[UIView setAnimationDidStopSelector:@selector(animateObject:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView setAnimationDuration:yourDelay];
[UIView commitAnimations];
animationPause == false;
}
-(void)animateStop:(NSString*)animationID finished:(BOOL)finished context:(void*)context{
}
【讨论】:
animationPause == false; 和 animationPause == true; 是比较,不做任何事情。您可能想改为分配值。
bicyclePad.animationPause == false; 仍然是一个比较,结果被扔掉了。你想要bicyclePad.animationPause = false;。注意==与=。