How to draw graphics in the gesture of iphone?

I have a question to draw a line or circle indicator after the user has pressed a stiffness gesture (i.e. the user has touched and dragged a finger) on the iPhone. However, UIGraphicsGetCurrentContext () always returns nil, does anyone know how to implement this on iPhone?

Thanks CLU

@interface MyView : UIView <UIGestureRecognizerDelegate> { CGPoint location; PanIndicator *panIndicator; } @implementation MyView - (id)init { if (self = [super init]) { UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [panGesture setMaximumNumberOfTouches:1]; [panGesture setDelegate:self]; [self addGestureRecognizer:panGesture]; [panGesture release]; panIndicator = [[PanIndicator alloc] init]; [self addSubview:panIndicator]; } return self; } - (void)panAction:(UIPanGestureRecognizer *)gR { if ([gR state]==UIGestureRecognizerStateBegan) { location = [gR locationInView:self]; } else if ([gR state]==UIGestureRecognizerStateEnded) { // The following code in this block is useless due to context = nil // CGContextRef context = UIGraphicsGetCurrentContext(); // CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0)); // CGContextStrokePath(context); } else if ([gR state]==UIGestureRecognizerStateChanged) { CGPoint location2 = [gR locationInView:self]; panIndicator.frame = self.bounds; panIndicator.startPoint = location; panIndicator.endPoint = location2; // [panIndicator setNeedsDisplay]; //I don't know why PanIncicator:drawRect doesn't get called [panIndicator drawRect:CGRectMake(0, 0, 100, 100)]; //CGRectMake is useless } } 
+1
source share
3 answers

You must follow the finger in the data part of the application. Call [myCanvasView setNeedsDisplay] in -(void)panAction:(UIPanGestureRecognizer *)gR and in the myCanvasView -drawInRect:(CGRect)rect method draw this track.

Something like that:

 - (void)panAction:(UIPanGestureRecognizer *)gR { [myData addPoint:[gR locationInView:gR.view]]; [myCanvasView setNeedsDisplay]; } - (void)drawInRect:(CGRect)rect { [self drawLinesFromData:myData]; } 

Draft for PanIndicator:

 @interface PanIndicator : UIView {} @property (nonatomic, assign) CGPoint startPoint; @property (nonatomic, assign) CGPoint endPoint; @end @implementation PanIndicator @synthesize startPoint = startPoint_; @synthesize endPoint = endPoint_; - (void)drawRect:(CGRect)aRect { [[UIColor redColor] setStroke]; UIBezierPath *pathToDraw = [UIBezierPath bezierPath]; [pathToDraw moveToPoint:self.startPoint]; [pathToDraw addLineToPoint:self.endPoint]; [pathToDraw stroke] } @end 
+1
source

Please follow the link and download the source code from there.

but it does not use UIGesturerecognizer ....

+1
source

I did this with a special gesture. When the gesture sets the state of the gestures (from the start, move, or end of the touch), the gesture action callback returns back to the view and the view calls "setNeedsDisplayInRect" and then the drawing comes from drawRect .

The catch with your implementation is that you cannot set the graphical context from gesture tracking methods. If the view indicates that you need to redraw (via "setNeedsDisplay"), this is done for you.

The reason for this is that the contents of the view can be cached in the layer, which is very important for optimizing the animation and layout. Therefore, if you need to draw a view, keep the rest of the interface in sync with your changes by calling setNeedsDisplay and executing the drawing from your drawRect method.

0
source

All Articles