我在touchesMoved中使用UIBezierPath绘制了一个形状。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
secondPoint = firstPoint;
firstPoint = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
CGPoint mid1 = midPoint(firstPoint, secondPoint);
CGPoint mid2 = midPoint(currentPoint, firstPoint);
[bezierPath moveToPoint:mid1];
[bezierPath addQuadCurveToPoint:mid2 controlPoint:firstPoint];
[self setNeedsDisplay];
}
在closePath之后,我想将红色填充颜色进去,但无法实现。请帮忙!
- (void)drawRect:(CGRect)rect
{
UIColor *fillColor = [UIColor redColor];
[fillColor setFill];
UIColor *strokeColor = [UIColor blueColor];
[strokeColor setStroke];
[bezierPath closePath];
[bezierPath fill];
[bezierPath stroke];
}
setFill
方法并没有像你期望的那样设置填充上下文(参见:https://stackoverflow.com/questions/10623545/uicolor-setfill-doesnt-work/10623961)。相反,使用 obj-C 的等效方法来设置填充,如下所示:let context = UIGraphicsGetCurrentContext()!; context.setFillColor(UIColor.blue.cgColor)
。 - pseudosudo