2

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!

Eilon
  • 2,698
  • 3
  • 16
  • 32
  • I imagine it relates to coordinates while under a transformation. Perhaps you should scale using CGAffineTransformScale rather than changing the frame? – Rory McKinnel Aug 28 '15 at 15:10
  • This link seems useful. The transform will be changing the coordinate system, so you need to convert coords back, do the changes then reapply the transform. http://stackoverflow.com/questions/19523487/find-frame-coordinates-after-uiview-transform-is-applied-cgaffinetransform – Rory McKinnel Aug 28 '15 at 15:14

1 Answers1

2

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

Eilon
  • 2,698
  • 3
  • 16
  • 32