Gesture line drawing

I add a subview view using the following code

imageview = [[UIImageView alloc] initWithFrame:[holderView frame]]; [imageview setImage:cppobject->OutputImage]; imageview.contentMode = UIViewContentModeScaleAspectFit; [holderView addSubview:imageview]; holderView.contentMode = UIViewContentModeScaleAspectFit ; UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)]; [pinchRecognizer setDelegate:self]; [holderView addGestureRecognizer:pinchRecognizer]; [pinchRecognizer release]; UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)]; [rotationRecognizer setDelegate:self]; [holderView addGestureRecognizer:rotationRecognizer]; [rotationRecognizer release]; UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; [panRecognizer setMinimumNumberOfTouches:1]; [panRecognizer setMaximumNumberOfTouches:1]; [panRecognizer setDelegate:self]; [holderView addGestureRecognizer:panRecognizer]; [panRecognizer release]; -(void)scale:(id)sender { } -(void)rotate:(id)sender { } -(void)move:(id)sender { } -(void)tapped:(id)sender { } 

I need to draw a line when the user uses two fingers to pan and uilabel (green), as in the following figure

I looked. How to draw graphics in the gesture of iphone?

But I could not apply it in subview, especially the DrawInRect method

in panRecognizer (move) function I want to draw a line using

  UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ; NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]]; [Points addObject:value]; [holderView setNeedsDisplay]; NSLog(@"End of measuring") ; 

and I will use dots in dots to draw a line over all subzones in

 -(void)drawRect:(CGRect)rect { NSLog(@"Entered Draw In Rect"); if (Measuring) { [[UIColor redColor] setStroke]; UIBezierPath *pathToDraw = [UIBezierPath bezierPath]; for (int n = 1; n < [Points count] - 1 ; n++) { NSValue * value = [Points objectAtIndex:(NSInteger)n]; CGPoint point = [value CGPointValue]; [pathToDraw moveToPoint:point]; value = [Points objectAtIndex:(NSInteger)n+1]; point = [value CGPointValue]; [pathToDraw addLineToPoint:point]; } [pathToDraw stroke]; } } 

the problem is [holderView setNeedsDisplay]; never call or click drawRect any suggestion or help regarding this

any suggestion

+4
source share
1 answer

The image view, which is a preview, is drawn on top of the holder view. Image image is opaque. This means that when viewing the views, there is no part of the owner’s view that is actually visible, so the drawRect call is optimized.

Try to arrange the views in reverse order, so that the holder view is a sub-image. Then the image will be drawn, and the view of the holder will be drawn on top of it.

Also note that you must use the borders of the parent view as a preview frame.

 UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]]; 

Edit (add):

See http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1 , in particular in the section View Hierarchy and View Management:

“Visually, the contents of a subclause hide all or part of the contents of its parent view”

So, try to make the figurative representation a parent, do your initialization as follows:

 // instance variables: UIImageView* imageView; MyHolderView* holderView; imageView = [[UIImageView alloc] initWithFrame:mainRect]; holderView = [[MyHolderView alloc] initWithFrame:[imageView bounds]]; holderView.opaque = NO; holderView.backgroundColor = [UIColor clearColor]; [imageView addSubview:holderView]; UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:holderView action:@selector(scale:)]; [pinchRecognizer setDelegate:self]; [holderView addGestureRecognizer:pinchRecognizer]; [pinchRecognizer release]; // etc... 

Now the image will be drawn, and the view of the holder, its preview will be drawn on top of it. Now, when you call setNeedsDisplay in holderview, it will get drawRect: call.

For example, track a gesture this way. It can be in your view controller or in a subclass of View MyHolderView; an example here will be in the MyHolderView class, so the instance variables location1 and location2 can be easily separated using the drawRect: method:

 -(void)scale:(id)sender { if (sender == pinchRecognizer) { // this allows the responder to work with multiple gestures if required // get position of touches, for example: NSUInteger num_touches = [pinchRecognizer numberOfTouches]; // save locations to some instance variables, like `CGPoint location1, location2;` if (num_touches >= 1) { location1 = [pinchRecognizer locationOfTouch:0 inView:holderView]; } if (num_touches >= 2) { location2 = [pinchRecognizer locationOfTouch:1 inView:holderView]; } // tell the view to redraw. [holderView setNeedsDisplay]; } } 

and then in manual drawRect mode:

 -(void)drawRect:(CGRect)rect { // use instance variables location1 and location2 to draw the line. } 
+4
source

All Articles