2014-12-06 102 views
0

我試圖創建,如果我有一個UIView與黃色背景的東西,而這種觀點在屏幕上移動,並置於某處另一個的UIView與紅色背景。我怎麼知道黃色是否碰到紅色? - 這意味着第一個觀點觸及另一個觀點。如何知道顏色「觸摸」另一種顏色

+0

可以使用'CGRectIntersectsRect'功能,與意見框架檢查交集。 – AMI289 2014-12-06 12:26:40

回答

0

期間,您將有定期檢查像@ 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 
    } 
} 

獲取動畫視圖的表示層非常重要,否則您將無法檢測視圖在屏幕上位置的增量更改。您還需要一旦你用它做,否則你的​​3210方法將繼續無限期運行無效的計時器。

相關問題