【问题标题】:Resizing subview of rotated UIView调整旋转 UIView 的子视图大小
【发布时间】:2015-08-28 14:56:26
【问题描述】:

我创建了一个具有调整大小、旋转和移动功能的 UIView 子类。我有一个 UITextView 作为子视图,它必须与父级一起移动、旋转和调整大小。一切正常,直到我旋转视图,然后调整它的大小;子视图开始出现这样的故障:https://gyazo.com/1341614679ada42311713cd53c1516ec(忽略旋转故障)

是什么导致了这个问题,如何解决?

这是我正在使用调整大小和旋转的代码:

// 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)

    }
}

提前致谢!

【问题讨论】:

  • 我想它与变换下的坐标有关。也许您应该使用 CGAffineTransformScale 进行缩放而不是更改框架?
  • 这个链接看起来很有用。变换将改变坐标系,因此您需要将坐标转换回来,进行更改然后重新应用变换。 stackoverflow.com/questions/19523487/…

标签: ios swift uiview rotation subview


【解决方案1】:

好的,我解决了,问题只是我在调整子视图的框架大小时使用了frame 而不是bounds。例如:

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

应该是

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

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2013-04-12
    • 2012-08-28
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多