Apple Dog
Apple Dog

Reputation: 35

How to make the width of the border the same after scaling?

I set "borderwidth = 3" to the view. After I scaling, the border either became thicker or thinner.

before scaling (yellow border on the left photo) after scaling( yellow border on the right photo) enter image description here

How can I keep the width of the border fixed after I scale it?

I user this source on the GitHub https://github.com/yokurin/DragRotateScaleView

And just add v.layer.borderWidth = 5

lazy var rect1: DragRotateScaleView = {
        let v = DragRotateScaleView(frame: CGRect(x: 20, y: 100, width: 200, height: 200))
        v.delegate = self
        v.backgroundColor = UIColor.cyan
        v.layer.borderColor = UIColor(red: 22/255, green:  22/255, blue:  22/255, alpha: 1).cgColor
        v.layer.borderWidth = 5
        return v
    }()

Thank You!!

Upvotes: 1

Views: 462

Answers (1)

Sergio
Sergio

Reputation: 1912

Just divide the border width by the scale and set that value to the borderWidth again.

Try this in a playground:

import UIKit
import PlaygroundSupport

// Save this values, you will use them.
let border: CGFloat = 3
let scale: CGFloat = 5

// Example views.
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
view.backgroundColor = .red
let secondView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
secondView.layer.borderColor = UIColor.yellow.cgColor
secondView.layer.borderWidth = border
view.addSubview(secondView)

// View scale transformation.
secondView.transform = CGAffineTransform(scaleX: scale, y: scale)

// IMPORTANT: Change the width of the border after the transformation.
secondView.layer.borderWidth = border / scale

// This is only for the playground.
PlaygroundPage.current.liveView = view

Upvotes: 1

Related Questions