iphone – 用我的手指在iOS中绘制贝塞尔曲线?

iphone – 用我的手指在iOS中绘制贝塞尔曲线?,第1张

概述嘿,我正在试图弄清楚如何根据用户输入在iOS中生成贝塞尔曲线.这有什么现有的课程吗?有人可以给我一个总体概要,说明需要什么吗?我只是需要帮助才能开始右脚. 如果你想留在 objective-c,你可以使用UIBezierPath的addCurveToPoint:controlPoint1:controlPoint2:方法.您还可以使用具有CGPaths的类似命名的函数.使用贝塞尔曲线时,需要4个点 嘿,我正在试图弄清楚如何根据用户输入在iOS中生成贝塞尔曲线.这有什么现有的课程吗?有人可以给我一个总体概要,说明需要什么吗?我只是需要帮助才能开始右脚.解决方法 如果你想留在 objective-c,你可以使用UIBezIErPath的addCurvetoPoint:controlPoint1:controlPoint2:方法.您还可以使用具有CGPaths的类似命名的函数.使用贝塞尔曲线时,需要4个点:起点,终点和每端的控制点来定义曲线.

定义此方法的一种方法是让用户拖动手指以定义起点和终点,然后点击控制点处的屏幕.这是一个处理此问题的示例视图.

BezIErVIEw.h

enum {    BezIErStateNone = 0,BezIErStateDefiningline,BezIErStateDefiningCP1,BezIErStateDefiningCP2};@interface BezIErVIEw : UIVIEw {    CGPoint startPt,endPt,cPt1,cPt2;    UInt8 state;    UIBezIErPath *curvePath;  @private    UItouch *currenttouch;}@property (nonatomic,retain) UIBezIErPath *curvePath;@end

BezIErVIEw.m

@interface BezIErVIEw@dynamic curvePath;- (UIBezIErPath *)curvePath {    return [[curvePath retain] autorelease];}- (voID)setCurvePath:(UIBezIErPath *)newPath {    ID tmp = curvePath;    curvePath = [newPath retain];    [tmp release];    state = BezIErStateNone;    [self setNeedsdisplay];}- (voID)_updateCurve {    UIBezIErPath *path = [UIBezIErPath bezIErPath];    [path movetoPoint:startPt];    [path addCurvetoPoint:endPt controlPoint1:cPt1 controlPoint2:cPt2];}- (voID)_calcDefaultControls {    if(ABS(startPt.x - endPt.x) > ABS(startPt.y - endPt.y)) {        cPt1 = (CGPoint){(startPt.x + endPt.x) / 2,startPt.y};        cPt2 = (CGPoint){cPt1.x,endPt.y};    } else {        cPt1 = (CGPoint){startPt.x,(startPt.y + endPt.y) / 2};        cPt2 = (CGPoint){endPt.x,cPt1.y};    }}- (voID)drawRect:(CGRect)rect {    UIBezIErPath *path = self.curvePath;    if(path) [path stroke];}- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    if(currenttouch) return;    if(state == BezIErStateNone) {        state = BezIErStateDefiningline;        currenttouch = [touches anyObject];        startPt = [currenttouch locationInVIEw:self];    } else if(state == BezIErStateDefiningCP1) {        currenttouch = [touches anyObject];        cPt1 = [currenttouch locationInVIEw:self];        [self _updateCurve];    } else if(state == BezIErStateDefiningCP2) {        currenttouch = [touches anyObject];        cPt2 = [currenttouch locationInVIEw:self];        [self _updateCurve];    }}- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    if(!currenttouch) return;    if(state == BezIErStateDefiningline) {        endPt = [currenttouch locationInVIEw:self];        [self _calcDefaultControls];        [self _updateCurve];    } else if(state == BezIErStateDefiningCP1) {        cPt1 = [currenttouch locationInVIEw:self];        [self _updateCurve];    } else if(state == BezIErStateDefiningCP2) {        cPt2 = [currenttouch locationInVIEw:self];        [self _updateCurve];    }}- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    if(!currenttouch) return;    if(state == BezIErStateDefiningline) {        state = BezIErStateDefiningCP1;    } else if(state == BezIErStateDefiningCP1) {        state = BezIErStateDefiningCP2;    } else if(state == BezIErStateDefiningCP2) {        state = BezIErStateNone;    }    currenttouch = nil;}- (voID)touchesCanceled:(NSSet *)touches withEvent:(UIEvent *)event {    if(state == BezIErStateDefiningline) {        self.curvePath = nil;        self.state = BezIErStateNone;    }    self.currenttouch = nil;}
总结

以上是内存溢出为你收集整理的iphone – 用我的手指在iOS中绘制贝塞尔曲线?全部内容,希望文章能够帮你解决iphone – 用我的手指在iOS中绘制贝塞尔曲线?所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存