简体   繁体   中英

UIBezierPath Not Drawing

I'm trying to implement a method which draws a line based on gesture recognition but I can't get the UIBezierPath to display. I know the gesture recognizer is functioning because I print to log every time the method is activated. What is also confusing me is that the blue line that I draw previous to trying to draw the BezierPath displays but the BezierPath doesn't. Even if I manually add arbitrary points nothing is drawn, such as:

[myPath addLineToPoint:CGPointMake(50, 50)];

Here is the code in my UIView:

- (void)drawRect:(CGRect)rect
{

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetRGBStrokeColor(ctx, 0, 0, 1.0, 1); 
CGContextMoveToPoint(ctx, 0, 150);
CGContextAddLineToPoint( ctx, 480, 150);
CGContextStrokePath(ctx);

[myPath addLineToPoint:CGPointMake(50, 50)];
[myPath stroke];
}

- (IBAction)handlePan:(UIPanGestureRecognizer *) recognizers {

CGPoint translation = [recognizers translationInView:self];
NSLog(@"Logged");

[myPath addLineToPoint:CGPointMake(translation.x, translation.y)];
[self setNeedsDisplay];
}

Thanks for any help!

Where are you initializing myPath? Make sure that it is initialized.

And just like

CGContextMoveToPoint(ctx, 0, 150);

, you need to call move to point on myPath before adding the line

[myPath moveToPoint:CGPointMake(0,150)];
[myPath addLineToPoint:CGPointMake(50, 50)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM