xcode UIBezierPath 用于手指触摸绘制的平滑曲线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11449677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIBezierPath Smooth Curve for finger touch drawing
提问by Dinesh
i want to smooth curve on finger touch draw line and i want solution in UIBezierPath only my code not smoothing line completely.my code here's
我想平滑手指触摸绘制线上的曲线,我想要 UIBezierPath 中的解决方案,只有我的代码不完全平滑线。我的代码在这里
@implementation MyLineDrawingView
@synthesize undoSteps;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[super setBackgroundColor:[UIColor whiteColor]];
pathArray=[[NSMutableArray alloc]init];
bufferArray=[[NSMutableArray alloc]init];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[[UIColor blackColor] setStroke];
for (UIBezierPath *_path in pathArray)
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[bufferArray removeAllObjects];
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth=5;
myPath.miterLimit=-10;
myPath.lineCapStyle = kCGLineCapRound;
myPath.flatness = 0.0;
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
[pathArray addObject:myPath];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)undoButtonClicked
{
if([pathArray count]>0)
{
UIBezierPath *_path=[pathArray lastObject];
[bufferArray addObject:_path];
[pathArray removeLastObject];
[self setNeedsDisplay];
}
}
-(void)redoButtonClicked
{
if([bufferArray count]>0){
UIBezierPath *_path=[bufferArray lastObject];
[pathArray addObject:_path];
[bufferArray removeLastObject];
[self setNeedsDisplay];
}
}
- (void)dealloc
{
[pathArray release];
[bufferArray release];
[super dealloc];
}
@end
and i am getting output using my code like below screen shot:
我使用我的代码获取输出,如下面的屏幕截图:
i want smooth curve output like below screen shot:
我想要平滑的曲线输出,如下面的屏幕截图:
can any one help me greatly appreciated!
任何人都可以帮助我,不胜感激!
Thanks in Advance!
提前致谢!
回答by Martin Kenny
The problem is that you're only adding points to the path using addLineToPoint:
. That's the equivalent of creating a series of straight lines. You really need to add control points, like the handles you might drag while drawing curved paths in Illustrator or Sketch.
问题是您只是使用addLineToPoint:
. 这相当于创建一系列直线。您确实需要添加控制点,例如在 Illustrator 或 Sketch 中绘制曲线路径时可能会拖动的手柄。
You can use addCurveToPoint:controlPoint1:controlPoint2:
to add those; the trick is working out where to put the control points, relative to the points you actually get back from the touches*
events.
您可以使用addCurveToPoint:controlPoint1:controlPoint2:
添加那些;诀窍是确定控制点的放置位置,相对于您从touches*
事件中实际获得的点。
For a good discussion of possible techniques, I recommend the following question: Drawing Smooth Curves - Methods Needed.
为了更好地讨论可能的技术,我推荐以下问题:绘制平滑曲线 - 需要的方法。