我想在视图上画线,这是我的代码。
- (void)drawRect:(CGRect)rect {
[self.image drawInRect:self.bounds];
}
- (void)drawNewLine {
UIGraphicsBeginImageContext(self.bounds.size);
[self.image drawInRect:self.bounds];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(ctx, 0.51, 0.85, 0.81, 1);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0);
CGContextBeginPath(ctx);
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGContextMoveToPoint(ctx, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(ctx, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[recognizer resetTouches];
[recognizer addTouches:touches fromView:self];
self.image = nil;
previousPoint1 = previousPoint2 = currentPoint = [[touches anyObject] locationInView:self];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
previousPoint2 = previousPoint1;
previousPoint1 = currentPoint;
currentPoint = [[touches anyObject] locationInView:self];
[self drawNewLine];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self drawNewLine];
[self processGestureData];
}
除了iPhone +之外,它在其他设备上运行良好,在6 / 6s / 7 plus上,你可以看到明显的延迟。有人可以告诉我我做错了什么。非常感谢。
这是我的应用。 CallHoney