缩放后如何使边框的宽度相同?

问题描述

我为视图设置了“ borderwidth = 3”。我缩放后,边框变得更粗或更细。

缩放之前(左照片上的黄色边框) 缩放后(右侧照片上的黄色边框) enter image description here

缩放边框后如何保持边框的宽度固定?

我在GitHub上使用了此源 https://github.com/yokurin/DragRotateScaleView

然后只需添加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
    }()

谢谢!

解决方法

只需将边框宽度除以比例,然后再次将该值设置为borderWidth

在操场上尝试:

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