IOS:从线到贝塞尔曲线的动画转换

IOS:从线到贝塞尔曲线的动画转换,第1张

概述我想给一条直线弯曲成一个比较直线的曲线(从“_”到“n”),是否有图书馆可以帮助我做这件事? 我知道如何用UIBezierPath绘制贝塞尔曲线,我可以快速重绘并逐渐进行转换,但是如果已经做到了,那将会很酷:-) 我可以用CADisplayLink做一些事情.例如,您可以使用CAShapeLayer在您的视图控制器中执行此 *** 作,例如: #import "ViewController.h"#imp 我想给一条直线弯曲成一个比较直线的曲线(从“_”到“n”),是否有图书馆可以帮助我做这件事?

我知道如何用UIBezIErPath绘制贝塞尔曲线,我可以快速重绘并逐渐进行转换,但是如果已经做到了,那将会很酷:-)

解决方法 我可以用CAdisplaylink做一些事情.例如,您可以使用CAShapeLayer在您的视图控制器中执行此 *** 作,例如:
#import "VIEwController.h"#import <QuartzCore/QuartzCore.h>@interface VIEwController ()@property (nonatomic) CFTimeInterval firstTimestamp;@property (nonatomic,strong) CAShapeLayer *shapeLayer;@property (nonatomic,strong) CAdisplaylink *displaylink;@property (nonatomic) NSUInteger loopCount;@endstatic CGfloat const kSeconds = 5.0;@implementation VIEwController- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    [self addShapeLayer];    [self startdisplaylink];}- (voID)addShapeLayer{    self.shapeLayer = [CAShapeLayer layer];    self.shapeLayer.path = [[self pathAtInterval:0.0] CGPath];    self.shapeLayer.fillcolor = [[UIcolor clearcolor] CGcolor];    self.shapeLayer.linewidth = 3.0;    self.shapeLayer.strokecolor = [[UIcolor redcolor] CGcolor];    [self.vIEw.layer addSublayer:self.shapeLayer];}- (voID)startdisplaylink{    self.displaylink = [CAdisplaylink displaylinkWithTarget:self selector:@selector(handledisplaylink:)];    [self.displaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];}- (voID)stopdisplaylink{    [self.displaylink invalIDate];    self.displaylink = nil;}- (voID)handledisplaylink:(CAdisplaylink *)displaylink{    if (!self.firstTimestamp)        self.firstTimestamp = displaylink.timestamp;    self.loopCount++;    NSTimeInterval elapsed = (displaylink.timestamp - self.firstTimestamp);    self.shapeLayer.path = [[self pathAtInterval:elapsed] CGPath];    if (elapsed >= kSeconds)    {        [self stopdisplaylink];        self.shapeLayer.path = [[self pathAtInterval:0] CGPath];        self.statusLabel.text = [Nsstring stringWithFormat:@"loopCount = %.1f frames/sec",self.loopCount / kSeconds];    }}- (UIBezIErPath *)pathAtInterval:(NSTimeInterval) interval{    UIBezIErPath *path = [UIBezIErPath bezIErPath];    [path movetoPoint:CGPointMake(0,self.vIEw.bounds.size.height / 2.0)];    CGfloat fractionOfSecond = interval - floor(interval);    CGfloat yOffset = self.vIEw.bounds.size.height * sin(fractionOfSecond * M_PI * 2.0);    [path addCurvetoPoint:CGPointMake(self.vIEw.bounds.size.wIDth,self.vIEw.bounds.size.height / 2.0)            controlPoint1:CGPointMake(self.vIEw.bounds.size.wIDth / 2.0,self.vIEw.bounds.size.height / 2.0 - yOffset)            controlPoint2:CGPointMake(self.vIEw.bounds.size.wIDth / 2.0,self.vIEw.bounds.size.height / 2.0 + yOffset)];    return path;}@end

或者,如果你想通过子类化UIVIEw来实现,你可以这样做:

#import "VIEw.h"#import <QuartzCore/QuartzCore.h>@interface VIEw ()@property (nonatomic,strong) CAdisplaylink *displaylink;@property (nonatomic) CFTimeInterval firstTimestamp;@property (nonatomic) CFTimeInterval displaylinkTimestamp;@property (nonatomic) NSUInteger loopCount;@endstatic CGfloat const kSeconds = 5.25;@implementation VIEw- (ID)initWithCoder:(NSCoder *)aDecoder{    self = [super initWithCoder:aDecoder];    if (self) {        [self startdisplaylink];    }    return self;}- (voID)startdisplaylink{    _displaylink = [CAdisplaylink displaylinkWithTarget:self selector:@selector(handledisplaylink:)];    [_displaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];}- (voID)stopdisplaylink{    [self.displaylink invalIDate];    self.displaylink = nil;}- (voID)handledisplaylink:(CAdisplaylink *)displaylink{    if (!self.firstTimestamp)        self.firstTimestamp = displaylink.timestamp;    self.displaylinkTimestamp = displaylink.timestamp;    self.loopCount++;    [self setNeedsdisplayInRect:self.bounds];    NSTimeInterval elapsed = (displaylink.timestamp - self.firstTimestamp);    if (elapsed >= kSeconds)    {        [self stopdisplaylink];        self.displaylinkTimestamp = self.firstTimestamp + kSeconds;        [self setNeedsdisplayInRect:self.bounds];        self.statusLabel.text = [Nsstring stringWithFormat:@"loopCount = %.1f frames/sec",self.bounds.size.height / 2.0)];    CGfloat fractionOfSecond = interval - floor(interval);    CGfloat yOffset = self.bounds.size.height * sin(fractionOfSecond * M_PI * 2.0);    [path addCurvetoPoint:CGPointMake(self.bounds.size.wIDth,self.bounds.size.height / 2.0)            controlPoint1:CGPointMake(self.bounds.size.wIDth / 2.0,self.bounds.size.height / 2.0 - yOffset)            controlPoint2:CGPointMake(self.bounds.size.wIDth / 2.0,self.bounds.size.height / 2.0 + yOffset)];    return path;}- (voID)drawRect:(CGRect)rect{    NSTimeInterval elapsed = (self.displaylinkTimestamp - self.firstTimestamp);    UIBezIErPath *path = [self pathAtInterval:elapsed];    [[UIcolor redcolor] setstroke];    path.linewidth = 3.0;    [path stroke];}@end

我测试了子类UIVIEw以及视图控制器,并且它们每秒产生大约60帧.

总结

以上是内存溢出为你收集整理的IOS:从线到贝塞尔曲线的动画转换全部内容,希望文章能够帮你解决IOS:从线到贝塞尔曲线的动画转换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112000.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存