2014-09-12 106 views
1

我想使用UIPinchGeustureRecognizer畫線,我嘗試了所有的stackoverflow解決方案,但沒有運氣。請幫我解決這個問題。我得到以下錯誤使用UIPinchGeustureRecognizer繪製線條

首先,我想知道我的代碼邏輯是否正確,我沒有從touchbegan/touchmoved獲得點。我從(void)handleLinePinch獲取兩點:(UIPinchGestureRecognizer *)手勢而已。

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

回答

2

你必須繼承的UIView並覆蓋drawRect:方法,你UIGraphicsGetCurrentContext得到CGContextRef無效出來drawRect:方法,並沒有建立一個強有力的參考圖形上下文,因爲它可以調用之間改變drawRect:方法。

當您識別捏合手勢時,將CGPoint傳遞給視圖並將setNeedsDisplay方法發送給它。

總是使用setNeedsDisplay刷新視圖,請不要直接發送drawRect:

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

編輯:我假設你只畫線的時候兩個手指上。

- (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

從哪裏讀我更新的location1和location2值?我的意思是touchesBegan/Moved或from(void)handleLinePinch?來自'handleLinePinch:'的 – user3714144 2014-09-17 07:17:25

+0

@ user3714144。 – KudoCC 2014-09-17 07:49:40

+0

我已經完成了,它的工作正常..事情是如何檢測繪製線拖動線。 – user3714144 2014-09-18 06:26:52