在CAAnimation中暂停动画
- (void)removeAnimationForKey:(NSString *)key;
- (void)removeAllAnimations;//计算当前视图位置
-(void)resetViewWithPosition:(float)position
{
CGRect rect = self.animateView.frame;
if (self.isVertical)
{
rect.origin.y = position;
}
else
{
rect.origin.x = position;
}
self.animateView.frame = rect;
}
//暂停动画
- (void)nx_pauseAnimate
{
if (!_isStart || _isPause)
{
return;
}
_isPause = YES;
[self.animateView.layer removeAllAnimations];
//动画已经进行的的时间
_animateTime = _animateTime + [[NSDate date] timeIntervalSince1970] - _startTime;
//当前的位置
float pausePosition = [_animation positionWithPauseTime:_animateTime];
float scap = (self.isVertical ? CGRectGetHeight(self.animateView.frame) :CGRectGetWidth(self.animateView.frame) )/2.;
[self resetViewWithPosition:(pausePosition -scap)];
}
//继续动画
- (void)nx_resumeAnimate
{
if (!_isStart || !_isPause)
{
return;
}
_isPause = NO;
//继续动画
_startTime = [[NSDate date] timeIntervalSince1970];
NSArray * keyPathArray = [_animation keyPathArrayWithPauseTime:_animateTime];
CAKeyframeAnimation * animate = [CAKeyframeAnimation animationWithKeyPath:_animation.keyPath];
animate.values = keyPathArray;
animate.duration = _animation.duration - _animateTime;
[self.animateView.layer addAnimation:animate forKey:_animation.keyPath];
[self resetViewWithPosition:_endPosition];
}全局时间
暂停/继续动画
Last updated