我正在做一项与数学相关的活动,用户可以在其中尝试解决数学问题时用手指绘制草图。但是,我注意到当我快速移动手指时,线条明显滞后于我的手指。我想知道是否有一些地方我忽略了性能,或者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。任何关于我如何能加快速度的建议将不胜感激。