2015-08-28 131 views
2

我創建了一個具有調整大小,旋轉和移動功能的UIView子類。我有一個UITextView作爲子視圖,必須移動,旋轉和調整父級。一切工作正常,直到我旋轉視圖,然後調整它;子視圖開始如此故障:https://gyazo.com/1341614679ada42311713cd53c1516ec(忽略旋轉毛刺)調整旋轉UIView的子視圖的大小

是什麼原因導致這個問題,它如何解決?

下面的代碼我使用調整大小和旋轉:

// Resizing 
func scale(recognizer: UILongPressGestureRecognizer) { 

    if recognizer.state == UIGestureRecognizerState.Changed { 

     let point = recognizer.locationInView(self) 
     var wChange = CGFloat(0.0), hChange = CGFloat(0.0) 

     wChange = point.x - self.prevPoint.x 
     let wRatioChange = wChange/bounds.size.width 

     hChange = wRatioChange * bounds.size.height 

     if abs(wChange) > 50.0 || abs(hChange) > 50.0 { 
      self.prevPoint = recognizer.locationOfTouch(0, inView: self) 
      return 
    } 

    bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width+wChange, bounds.size.height+hChange) 

    self.prevPoint = recognizer.locationOfTouch(0, inView: self) 

    update() // Updating subviews frames 
} 

func update() { 
    // resizing of other subviews 
    deleteButton.center.x = self.frame.width - deleteButton.frame.width * 0.5 
    deleteButton.frame.origin.y = 0 
    sizeButton.frame.origin.x = self.frame.width - sizeButton.frame.width 
    sizeButton.frame.origin.y = self.frame.height - sizeButton.frame.height 

    self.theTextView.frame.size = CGSize(width: self.frame.width - self.deleteButton.frame.width*2*0.66666666,height: self.frame.height - self.deleteButton.frame.height) 

    //vRotate.frame.origin.x = 0 
    //vRotate.frame.origin.y = self.frame.height - sizeButton.frame.height 
    self.theTextView.center = CGPoint(x: self.frame.width/2,y: self.frame.height/2) 
    updateTextFont(self.theTextView) 
    self.theTextView.textAlignment = NSTextAlignment.Center 

    ... 
} 

旋轉功能:

func rotate(recognizer: UIPanGestureRecognizer) { 
    if recognizer.state == .Changed { 
     let ang = atan2(recognizer.locationInView(self.superview).y - center.y, recognizer.locationInView(self.superview).x - center.x) 
     print(ang) 
     let angleDiff = self.deltaAngle - ang 

     self.transform = CGAffineTransformMakeRotation(-angleDiff) 

    } 
} 

提前感謝!

+0

我想這涉及到協調而變換下。也許你應該使用CGAffineTransformScale進行縮放而不是改變框架? –

+0

此鏈接似乎很有用。該轉換將改變座標系,因此您需要將座標轉換回來,執行更改並重新應用轉換。 http://stackoverflow.com/questions/19523487/find-frame-coordinates-after-uiview-transform-is-applied-cgaffinetransform –

回答

2

好吧,我解決了它,問題是簡單的,我使用frame而不是bounds調整子視圖的框架。例如:

sizeButton.frame.origin.y = self.frame.height - sizeButton.frame.height

應該

sizeButton.frame.origin.y = self.bounds.height - sizeButton.frame.height

相關問題