【问题标题】:How to know if color "touch" another color如何知道颜色是否“接触”另一种颜色
【发布时间】:2015-02-04 13:10:10
【问题描述】:

我正在尝试创建一些东西,如果我有一个黄色背景的 UIView,并且这个视图在屏幕上移动,并且在某个地方放置了另一个红色背景的 UIView。我怎么知道黄色是否碰到红色? - 这意味着第一个视图触及另一个视图。

【问题讨论】:

  • 您可以使用CGRectIntersectsRect 函数,与视图的框架一起检查相交。

标签: ios objective-c uiview colors


【解决方案1】:

在动画期间,您必须像@AMI289 所说的那样定期检查交叉点。例如

- (void)animate {
    // Use an NSTimer to check for intersection 10 times per second
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkForCollision) userInfo:nil repeats:YES];
    [UIView animateWithDuration:5 animations:^{
        // Do the animation
    } completion:^(BOOL complete) {
        // Tell timer to stop calling checkForCollision
        [timer invalidate];
    }];
}


- (void)checkForCollision {
    if (CGRectIntersectsRect([viewOne.layer.presentationLayer frame], [viewTwo.layer.presentationLayer frame])) {
        // Handle collision
    }
}

获取动画视图的表示层很重要,否则您将无法检测到视图在屏幕上位置的增量变化。完成后,您还需要使计时器无效,否则您的 checkForCollision 方法将继续无限期运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多