简体   繁体   中英

Resizing subview of rotated UIView

I've created a UIView subclass that has resizing, rotation and movement functions. I have a UITextView as a subview, that has to move, rotate and resize with the parent. Everything works fine, until I rotate the view, and then resize it; The subviews starts to glitch like so: https://gyazo.com/1341614679ada42311713cd53c1516ec (ignore the rotation glitch)

What causes this problem and how can it be fixed?

Here's the code I'm using resizing and rotating:

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

    ... 
}

Rotation function:

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)

    }
}

Thanks in advance!

Ok I solved it, the problem was simply that I used frame instead of bounds when resizing the subviews' frames. For example:

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

Should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM