Draw Line Using UIPinchGeustureRecognizer

后端 未结 1 1324
星月不相逢
星月不相逢 2021-01-16 09:23

I want to draw line using UIPinchGeustureRecognizer,I have tried all stackoverflow solution but no luck. please help me to resolve this. I am getting following error

相关标签:
1条回答
  • 2021-01-16 09:55

    You must subclass the UIView and override the drawRect: method, the CGContextRef you get with UIGraphicsGetCurrentContext is invalid out of drawRect: method and don't establish a strong reference to the graphics context because it can change between calls to the drawRect: method.

    When you recognize the pinch gesture, pass the CGPoint to the view and send setNeedsDisplay method to it.

    Always use setNeedsDisplay to refresh the view, don't send drawRect: directly.

    LineView.m

    - (void)drawRect:(CGRect)rect
    {
        // p1 and p2 should be set before call `setNeedsDisplay` method
        [self drawRect:location1 Loc2:location2] ;
    }
    
    - (void)drawRect:(CGPoint)location1 Loc2:(CGPoint)location2 {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 5.0);
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
        CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
        CGColorRef color = CGColorCreate(colorspace, components);
        CGContextSetStrokeColorWithColor(context, color);
        CGContextMoveToPoint(context, location1.x, location1.y);
        CGContextAddLineToPoint(context, location2.x, location2.y);
        CGContextStrokePath(context);
        CGColorSpaceRelease(colorspace);
        CGColorRelease(color);
    }
    

    Edit: I assume you only draw the line when two fingers are on.

    - (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture
    {
        NSUInteger num_touches = [gesture numberOfTouches];
    
        // save locations to some instance variables, like `CGPoint location1, location2;`
        if (num_touches == 2) {
           location1 = [gesture locationOfTouch:0 inView:l];
           location2 = [gesture locationOfTouch:1 inView:l];
        }
        // you need save location1 and location2 to `l` and refresh `l`.
        // for example: l.location1 = location1; l.location2 = location2;
        [l setNeedsDisplay];
    
    }
    
    0 讨论(0)
提交回复
热议问题