CAKeyframeAnimation手动进度
我有一个UIView的背景层有一个简单的直线pathCAKeyframeAnimation设置为其“path”。
我可以让animation“冻结”,也就是说,手动改变它的进度?
例如:如果path长度为100个点,则将进度(偏移量?)设置为0.45应使视图向下移动45个点。
我记得看到一篇文章,通过CAMediaTiming接口做了类似的事情(沿着一个基于滑块的值的path移动一个视图),但是我还没有find它,即使经过了几个小时的search。 如果我以完全错误的方式接近这一点,请让我知道。 谢谢。
这里有一些示例代码,如果以上不够清楚。
- (void)setupAnimation { CAKeyFrameAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:_label.layer.position]; [path addLineToPoint:(CGPoint){200, 200}]; animation.path = path.CGPath; animation.duration = 1; animation.autoreverses = NO; animation.removedOnCompletion = NO; animation.speed = 0; // _label is just a UILabel in a storyboard [_label.layer addAnimation:animation forKey:@"LabelPathAnimation"]; } - (void)sliderDidSlide:(UISlider *)slider { // move _label along _animation.path for a distance that corresponds to slider.value }
是的,您可以使用CAMediaTiming界面来做到这一点。 您可以将layer
的speed
设置为0
并手动设置timeOffset
。 一个简单的暂停/恢复方法的例子:
- (void)pauseAnimation { CFTimeInterval pausedTime = [yourLayer convertTime:CACurrentMediaTime() fromLayer:nil]; yourLayer.speed = 0.0; yourLayer.timeOffset = pausedTime; } - (void)resumeAnimation { CFTimeInterval pausedTime = [yourLaye timeOffset]; if (pausedTime != 0) { yourLayer.speed = 1.0; yourLayer.timeOffset = 0.0; yourLayer.beginTime = 0.0; CFTimeInterval timeSincePause = [yourLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; yourLayer.beginTime = timeSincePause; } }
这是基于乔纳森所说的,只是更重要的一点。 animation设置正确,但滑块操作方法应如下所示:
- (void)sliderDidSlide:(UISlider *)slider { // Create and configure a new CAKeyframeAnimation instance CAKeyframeAnimation *animation = ...; animation.duration = 1.0; animation.speed = 0; animation.removedOnCompletion = NO; animation.timeOffset = slider.value; // Replace the current animation with a new one having the desired timeOffset [_label.layer addAnimation:animation forKey:@"LabelPathAnimation"]; }
这将使标签基于timeOffset
沿着animationpath
移动。
- iOS在图像上滑动图像,显示下面的图像。 (A-la jQuery.slide())
- 为CGAffineTransformMakeRotation Swift设置旋转点
- UIKitdynamic:识别圆形形状和边界
- 如何检测由CATransaction触发的animation的完成?
- 核心animation – 修改animation属性
- 我怎样才能一个接一个地链接不同层的核心animation?
- 核心animationCALayer掩盖animationperformance
- iOS的animation/变形形状从圆形到方形
- UIView animateWithDuration不在ios8中animation