使用UIPinchGeustureRecognizer绘制线条 [英] Draw Line Using UIPinchGeustureRecognizer

查看:117
本文介绍了使用UIPinchGeustureRecognizer绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用UIPinchGeustureRecognizer绘制线,我已经尝试了所有stackoverflow解决方案,但没有运气。请帮我解决这个问题。我收到以下错误

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

首先我想知道我的代码逻辑是否正确。我没有从touchbegan / touchmoved获得积分。我只从(void)handleLinePinch :( UIPinchGestureRecognizer *)手势获得两分。

First i want to know my code logic is correct or not.and i didnt get the points from touchbegan/touchmoved. i am gettting two points from (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture only.

//My instances in .h file

CGPoint location1,location2;
LineView* l;

- (void)viewDidLoad
{
   [super viewDidLoad];
   l = [[LineView alloc]initWithFrame:self.view.frame];
   [self.view addSubview:l];
   UIPinchGestureRecognizer *linepinch = [[UIPinchGestureRecognizer alloc]    
   initWithTarget:l action:@selector(handleLinePinch:)];
   [l addGestureRecognizer:linepinch];
}


- (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture
{
    NSUInteger num_touches = [gesture numberOfTouches];

    // save locations to some instance variables, like `CGPoint location1, location2;`
    if (num_touches >= 1) {
       location1 = [gesture locationOfTouch:0 inView:l];
    }
    if (num_touches >= 2) {
       location2 = [gesture locationOfTouch:1 inView:l];
    }
    [l drawRect:location1 Loc2:location2];
    [l setNeedsDisplay];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}


LineView.m

- (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);
}


推荐答案

你必须继承 UIView 并覆盖 drawRect:方法,你得到的 CGContextRef UIGraphicsGetCurrentContext drawRect:方法中无效,并且不建立对图形上下文的强引用,因为它可以更改在调用drawRect:方法之间。

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.

当您识别到捏合手势时,将CGPoint传递给视图并发送 setNeedsDisplay 方法。

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

始终使用 setNeedsDisplay 刷新视图,不要发送 drawRect:直接。

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

LineView.m

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);
}

编辑:我假设您只在两根手指打开时画线。

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];

}

这篇关于使用UIPinchGeustureRecognizer绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆