我正在进行一项与数学相关的活动,用户可以用手指画画,因为他们试图解决数学问题。但是,我注意到当我快速移动手指时,线条略微明显地落在我的手指后面。我想知道是否有一些我忽略了性能的区域,或者touchesMoved
只是不够了(如果你不快速移动,那就非常顺利和美妙)。我正在使用UIBezierPath
。首先,我在我的init方法中创建它:
myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineCapSquare;
myPath.lineJoinStyle = kCGLineJoinBevel;
myPath.lineWidth=5;
myPath.flatness = 0.4;
然后在drawRect:
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
if(baseImageView.image)
{
CGContextRef c = UIGraphicsGetCurrentContext();
[baseImageView.layer renderInContext:c];
}
CGBlendMode blendMode = self.erase ? kCGBlendModeClear : kCGBlendModeNormal;
[myPath strokeWithBlendMode:blendMode alpha:1.0];
}
baseImageView是我用来保存结果的,所以我不必绘制很多路径(一段时间后变得很慢)。这是我的触摸逻辑:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
CGContextRef c = UIGraphicsGetCurrentContext();
[self.layer renderInContext:c];
baseImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[myPath removeAllPoints];
[self setNeedsDisplay];
}
该项目将作为企业应用程序发布,因此它只会安装在iPad 2上。目标iOS为5.0。任何有关如何从中获得更多速度的建议都将不胜感激。
答案 0 :(得分:3)
当然,您应该首先在“仪器”下运行它并查找您的热点。然后,您需要进行更改并重新评估以查看其影响。否则你只是在猜测。也就是说,经验中的一些注释:
addLineToPoint:
成为热门话题,我不会感到惊讶。它一直适合我。答案 1 :(得分:1)
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/
此链接准确显示了如何使曲线更平滑。本教程逐步展示。并简单地告诉我们如何在曲线中添加一些中间点(在touchesMoved方法中)以使它们更平滑。