你如何暂停和恢复UIView动画? (没有块动画)
我正在努力解决这个问题,所以以下是关于如何做到这一点的来源。
答案 0 :(得分:2)
如何暂停和恢复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{
}