gpt4 book ai didi

ios - 无法对齐 UIView 和 CAShapeLayer

转载 作者:可可西里 更新时间:2023-11-01 01:37:14 25 4
gpt4 key购买 nike

我在对齐 UIViewCAShapeLayer 时遇到问题。

这是演示问题的示例代码:

import UIKit

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
square.transform = CGAffineTransformMakeRotation(CGFloat(20*M_PI/180))
square.backgroundColor = UIColor.grayColor()
view.addSubview(square)

let strokeLayer = CAShapeLayer()
strokeLayer.path = UIBezierPath(rect: square.bounds).CGPath
strokeLayer.position = square.center
strokeLayer.setAffineTransform(square.transform)

strokeLayer.fillColor = UIColor.clearColor().CGColor
strokeLayer.strokeColor = UIColor.redColor().CGColor
strokeLayer.lineWidth = 1

view.addSubview(square)
view.layer.addSublayer(strokeLayer)
}
}

这是结果的图片:

Result of code in iOS Simulator

如何让两者对齐?

最佳答案

您唯一需要做的更改是添加:

strokeLayer.frame = square.layer.bounds

紧接着:

let strokeLayer = CAShapeLayer()

即,

let strokeLayer = CAShapeLayer()
strokeLayer.frame = square.layer.bounds
strokeLayer.path = UIBezierPath(rect: square.bounds).CGPath
strokeLayer.position = square.center
strokeLayer.setAffineTransform(square.transform)

strokeLayer.fillColor = UIColor.clearColor().CGColor
strokeLayer.strokeColor = UIColor.redColor().CGColor
strokeLayer.lineWidth = 1

关于ios - 无法对齐 UIView 和 CAShapeLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com