定义此方法的一种方法是让用户拖动手指以定义起点和终点,然后点击控制点处的屏幕.这是一个处理此问题的示例视图.
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中绘制贝塞尔曲线?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)