2016-05-23 54 views
0

我面臨着兩難的算法...展開碰撞UIViews

我開發一個iOS應用程序,我有一個半徑圖,其中,中間有我的資料圖片和角落找尋簡介圖像有其他來自其他用戶的檔案圖像。 事情是,這些配置文件中的一些重疊,我想擴大它們。

我的目標是獲得離我最近的配置文件,並檢查是否有其他配置文件重疊。如果是,則採用該配置文件並將其從另一配置文件移開。

The profiles overlaping

The profile moved from the nearest profile to the center

能有人幫我...

謝謝!

回答

0

我終於解決了這個代碼:

int tries = 0; 
    while(true){ 
     BOOL f = NO; 
     for(Dot *dot1 in dots){ 

      for(Dot *dot2 in dots){ 

       if(dot1 == dot2) 
        continue; 

       CGFloat distance = fabs([self distanceFromView:dot1 toView:dot2]); 

       if(distance < 36.0){ 

        f = YES; 

        CGFloat bearing = atan2((dot2.suggestedCenter.y - dot1.suggestedCenter.y) , (dot2.suggestedCenter.x - dot1.suggestedCenter.x)); 

        CGPoint vector = CGPointMake(36.0 * cosf(bearing), 36.0 * sinf(bearing)); 

        CGPoint newPoint = CGPointMake(dot1.suggestedCenter.x + vector.x, dot1.suggestedCenter.y + vector.y); 
        dot2.suggestedCenter = newPoint; 

       } 

      } 

     } 
     if(!f) 
      break; 
     tries ++; 
     if(tries > 100) 
      break; 
    } 

其中36.0是點和distanceFromView寬度:toView:是:

-(double)distanceFromView:(Dot *)from toView:(Dot *)to{ 
     return sqrt(pow(to.suggestedCenter.x - from.suggestedCenter.x, 2) + pow(to.suggestedCenter.y - from.suggestedCenter.y, 2)); 
    } 

希望幫助別人。