【问题标题】:CGContext and UIImageView Intersection detectionCGContext 和 UIImageView 交叉点检测
【发布时间】:2013-10-01 13:49:42
【问题描述】:

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

【问题讨论】:

    标签: ios uiimageview cgcontext


    【解决方案1】:

    在拖动时,您可以检查与 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
    }
    

    【讨论】:

    • 但是如何将 CGContext 转换为 CGRect?我添加了一些代码
    【解决方案2】:

    在您的 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

    希望这会有所帮助。

    【讨论】:

    • 我只是想知道它是否与绘制的线相交,而不是整个视图
    • 画出来的线不是UIView对象吗?
    • 是的,但您的函数会检查它是否与 UIView 发生冲突。我希望它检查它是否与线冲突
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多