2013-10-01 76 views
0

如何檢測我的繪製線是否與UIImageView相交?CGContext和UIImageView交集檢測

不知何故,互聯網並不是很有幫助。但預先感謝!

- (void)drawRect:(CGRect)rect { 

    context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGContextMoveToPoint(context, 384, 435); 
    CGContextAddLineToPoint(context, 494, 402); 
    CGContextAddLineToPoint(context, 537, 419); 
    CGContextAddLineToPoint(context, 544, 450); 
    CGContextAddLineToPoint(context, 494, 488); 

    NSLog(@"%@", NSStringFromCGRect(recting)); 

    CGContextStrokePath(context); 
} 

回答

0

拖動時,您可以檢查與UIImageView作爲交集。 1. CGRect frame = imageView.frame; 2.現在,如果當前拖動位置是框架的子集,則表示存在衝突。

CGPoint location = [touch locationInView:touch.view]; 
if(location.x>=frame.origin.x && location.x<= frame.origin.x+frame.size.width){ 
//collision with UIImageView 
} 
+0

但我怎樣才能將CGContext翻譯成CGRect?我添加了一些代碼 – AmiiQo

0

在您的viewDidLoad或viewDidAppear 附加平移手勢

UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panned:)]; 
[self.view addGestureRecognizer:pan]; 

**圖像平移各地**

-(void)panned:(UIPanGestureRecognizer *)panned 
{ 
    CGPoint currentPoint=[panned locationInView:self.imageView]; 
    CGFloat width=self.imageView.bounds.size.width; 
     CGFloat height=self.imageView.bounds.size.height; 
    CGRect newRect=CGRectMake(currentPoint.x, currentPoint.y, width, height); 
    if (CGRectIntersectsRect(newRect, line.frame)) { 
     NSLog(@"intersects"); 


    } 
    else{ 
     NSLog(@"doesnt interesct"); 
    } 


} 

line.frame是畫線了UIView

希望這有助於。

+0

以及不幸的是,這是行不通的 – AmiiQo

+0

我只是想找出它是否與畫出的線相交,而不是整個視圖 – AmiiQo

+0

是不是畫出的線UIView對象? – vin