大佬教程收集整理的这篇文章主要介绍了ios – 使用CADisplayLink’结合’与CAMediaTimingFunction动画UIView. (获得任意曲线),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
例如我有一个视图MyView,它有一个属性值,每当设置值时我调用setNeedsDisplay并且视图知道要绘制的内容.
为了动画这我使用CADisplayLink,我希望视图在值之间“变形”.我只是通过插入动画的开始和结束值来实现这一点:
– (CGFloat)interpolatedValue:(CGFloat)sourceValue withValue:(CGFloat)targetValue forProgress:(CGFloat)进展;
现在获得一个线性进展很容易并得到一个“特定曲线”(好),但我希望能够利用CAMediaTimingFunction来做到这一点(或者其他一些预先存在的逻辑 – 我不想再次重新发明轮子’: )
RSTiming
,在你的情况下可能会很方便.您可以使用标准CAMediaTimingFunction定义定时功能,甚至可以使用2个控制点定义定制功能来定义贝塞尔曲线.
如果我得到你的设置,你可能会有这样的事情:
视图控制器
#import "ViewController.h" #import "AnimatedView.h" #include <stdlib.h> @interface ViewController () @property (nonatomic,strong) CADisplayLink *displayLink; @property (weak,nonatomiC) IBOutlet AnimatedView *viewToAnimate; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.displayLink = [CADisplayLink displayLinkWithTarget:self SELEctor:@SELEctor(updateFramE)]; [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; } - (void)updateFrame { [self.viewToAnimate updateAnimation]; } - (IBACtion)updateAnimationTapped:(id)sender { self.viewToAnimate.value = arc4random_uniform(101) / 100.0; NSLog(@"Next animation value: %f",self.viewToAnimate.value); } @end
AnimatedView
#import "AnimatedView.h" #import "RSTimingFunction.h" @interface AnimatedView() { CGFloat _lastValue; CGFloat _progressStep; CGFloat _currentProgress; } @property (nonatomic,strong) RSTimingFunction *animationProgress; @end @implementation AnimatedView - (instanCETypE)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { _progressStep = 0.01; // defines animation speed _currentProgress = 1.0; self.animationProgress = [RSTimingFunction timingFunctionWithName:kRSTimingFunctionEaseInEaSEOut]; } return self; } - (void)SETVALue:(CGFloat)value { if (_value != value) { _lastValue = _value; _value = value; _currentProgress = 0.0; } } - (void)updateAnimation { if (_currentProgress > 1.0) return; _currentProgress += _progressStep; CGFloat currentAnimationValue = _lastValue + (_value - _lastvalue) * [self.animationProgress valueForX:_currentProgress]; self.alpha = currentAnimationValue; // as an example animate alpha } @end
如上所述,您甚至可以设置2个控制点来创建以三次贝塞尔曲线为模型的定时函数.
self.animationProgress = [RSTimingFunction timingFunctionWithControlPoint1:CGPointMake(0.6,0.6) controlPoint2:CGPointMake(0.1,0.8)];
这将产生以下时间动画(使用CAMediaTimingFunction playground制作)
以上是大佬教程为你收集整理的ios – 使用CADisplayLink’结合’与CAMediaTimingFunction动画UIView. (获得任意曲线)全部内容,希望文章能够帮你解决ios – 使用CADisplayLink’结合’与CAMediaTimingFunction动画UIView. (获得任意曲线)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。