0

如何检测我的绘制线是否与我拖动的 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);
}
4

2 回答 2

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

希望这可以帮助。

于 2013-10-01T14:57:55.593 回答
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
}
于 2013-10-01T14:12:55.190 回答