对齐UIView和CAShapeLayer的问题
问题描述:
我在对齐UIView
和CAShapeLayer
时遇到问题。对齐UIView和CAShapeLayer的问题
下面是示例代码演示该问题:
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)
}
}
下面是结果的图像:
如何获得两个对齐?
答
你需要做的只是改变你的形状层是什么补充:
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
答
如果位置(0,0)
strokeLayer.position = CGPointMake(0, 0)
没有。看起来像这样:http://imgur.com/YvHmZr8 – Trappar
也从您的形状图层中删除变换 – nielsbot
http://imgur.com/qh8xrWH – Trappar