2011-09-02 51 views
3

我想在另一個視圖內有一個可移動/可縮放/可旋轉的視圖。內部視圖可以超出外部視圖的框架,但我想保留它的一部分在外部視圖內,所以內部視圖不會丟失。如何在另一個UIView中保留一個旋轉的UIView? (框架不能使用)

我簡化了這個xcode項目中的問題https://github.com/nextorlg/Intersection

如果內部的觀點是唯一可移動的和可擴展的問題就已經解決了,但是當內部視圖旋轉,這種解決方案,因爲該框架包含了看法,但它是不是認爲自己這是不好的。

到我用這個功能來驗證新的視圖位置,如果它是無效的我回復最後轉換內部視圖執行的每個轉變(這是可移動的視圖代碼https://github.com/nextorlg/Intersection/blob/master/intersec/MovableView.m

-(BOOL) validInset { 
    CGRect outerLimit = CGRectMake(0, 0, self.superview.frame.size.width, self.superview.frame.size.height); 
    CGRect intersectionRect = CGRectIntersection(self.frame, outerLimit); 
    NSLog(@"self.frame:%f,%f,%f,%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height); 
    NSLog(@"outer.frame:%f,%f,%f,%f", outerLimit.origin.x, outerLimit.origin.y, outerLimit.size.width, outerLimit.size.height); 
    NSLog(@"intersec.frame:%f,%f,%f,%f", intersectionRect.origin.x, intersectionRect.origin.y, intersectionRect.size.width, intersectionRect.size.height); 
    NSLog(@"========================"); 
    if (CGRectIsNull(intersectionRect) || 
     intersectionRect.size.width < INSET || 
     intersectionRect.size.height < INSET) { 
     return NO; 
    } 
    else { 
     return YES; 
    } 
} 

問題是,我怎麼能確定它的內部視圖不會在第一次旋轉一些(例如45度)度並拖到角落時在外部視圖後面丟失?

一個評論,我想保持外觀圖裏面只是一些像素,因爲內部視圖可以比外一個更大的(按比例)。

我建議您下載並運行該項目,以更好地understan的問題,這是很難理解它只是閱讀本。

謝謝!

回答

1

如您所述,只有當內部視圖未旋轉時,您的方法纔會正常工作。

我知道解決您的問題,最簡單的方法是簡單地測試,如果中心或一個視圖的頂點是另一個裏面。

-(BOOL) validInset { 
    // Get the vertices of A (outerLimit) 
    CGRect outerLimit = self.superview.bounds; 
    CGPoint pointA[] = { 
     CGPointMake(outerLimit.origin.x, outerLimit.origin.y ), 
     CGPointMake(outerLimit.size.width, outerLimit.origin.y ), 
     CGPointMake(outerLimit.size.width, outerLimit.size.height), 
     CGPointMake(outerLimit.origin.x, outerLimit.size.height)}; 
    // Adjust outerLimit's borders 
    pointA[0].x += INSET, pointA[0].y += INSET; 
    pointA[1].x -= INSET, pointA[1].y += INSET; 
    pointA[2].x -= INSET, pointA[2].y -= INSET; 
    pointA[3].x += INSET, pointA[3].y -= INSET; 
    // Get the vertices of B (innerView) 
    CGRect innerView = self.bounds; 
    CGPoint pointB[] = { 
     CGPointMake(innerView.origin.x, innerView.origin.y ), 
     CGPointMake(innerView.size.width, innerView.origin.y ), 
     CGPointMake(innerView.size.width, innerView.size.height), 
     CGPointMake(innerView.origin.x, innerView.size.height)}; 
    // Test if the center of B is inside A or vice versa 
    CGPoint center, converted; 
    center = CGPointMake(pointB[0].x + (pointB[1].x - pointB[0].x)/2, pointB[0].y + (pointB[2].y - pointB[0].y)/2); 
    if(converted = [self convertPoint:center toView:self.superview], 
     converted.x >= pointA[0].x && 
     converted.x <= pointA[1].x && 
     converted.y >= pointA[0].y && 
     converted.y <= pointA[2].y) return YES; 
    center = CGPointMake(pointA[0].x + (pointA[1].x - pointA[0].x)/2, pointA[0].y + (pointA[2].y - pointA[0].y)/2); 
    if(converted = [self convertPoint:center toView:self.superview], 
     converted.x >= pointA[0].x && 
     converted.x <= pointA[1].x && 
     converted.y >= pointA[0].y && 
     converted.y <= pointA[2].y) return YES; 
    // Test if vertices of B are inside A or vice versa 
    for (int i = 0; i < 4; i++) { 
     if(converted = [self convertPoint:pointB[i] toView:self.superview], 
      converted.x >= pointA[0].x && 
      converted.x <= pointA[1].x && 
      converted.y >= pointA[0].y && 
      converted.y <= pointA[2].y) return YES; 
     if(converted = [self.superview convertPoint:pointA[i] toView:self], 
      converted.x >= pointB[0].x && 
      converted.x <= pointB[1].x && 
      converted.y >= pointB[0].y && 
      converted.y <= pointB[2].y) return YES; 
    } 
    return NO; 
} 

此代碼將完成處理您在示例中使用的轉換的工作,但它不是一個通用的答案。

非iPhone相關的回答這個問題,發現here

而且你還可以閱讀有關矩形,交點here一些背景。

編輯:

對於可以應用於UIView的轉換,以測試頂點和中心將爲案件是不夠的。

的兩個著名的例外是:

  • 當h <ħ/2並且w /2>瓦特(或h < h /2和w /2> w ),
  • 當2 *瓦特 <ħ/2和r >瓦特(或2 *瓦特 <ħ/2和r >瓦特);

其中

  • r爲UIView和一個頂點的中心之間的距離;
  • w = UIView最短邊的長度;
  • h = UIView最長邊的長度。

如果您遇到這些情況之一,則應使用其他方法,如first link中的方法。

+0

不太喜歡它,但工作... – Nextorlg