绘制带约束的形状 swift 4
Drawing shapes with constraints swift 4
我正在尝试学习如何绘制形状并为其设置动画,我在这两个方面都取得了成功并且我设法在屏幕中央绘制形状并且彼此对齐,但是当我将模拟器更改为任何除了我的设备之外的设备,它们跳出了屏幕的中心,有什么方法可以对我绘制的形状设置约束,使它们始终位于中心?
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// rect
let layer = CAShapeLayer()
let bounds = CGRect(x: 60, y: 200, width: 250, height: 250)
layer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 20, height: 20)).cgPath
layer.strokeColor = UIColor.white.cgColor
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 4
view.layer.addSublayer(layer)
let rectAnimation = CABasicAnimation(keyPath: "strokeEnd")
rectAnimation.fromValue = 0
rectAnimation.toValue = 1
rectAnimation.duration = 2
layer.add(rectAnimation, forKey: "line")
}
Masonry 是一个强大的约束工具,您会非常喜欢它。设置约束并为它们设置动画非常容易;
否则,您也可以将 x 和 y "bounds" 坐标设置为 centerX 和 centerY
我正在尝试学习如何绘制形状并为其设置动画,我在这两个方面都取得了成功并且我设法在屏幕中央绘制形状并且彼此对齐,但是当我将模拟器更改为任何除了我的设备之外的设备,它们跳出了屏幕的中心,有什么方法可以对我绘制的形状设置约束,使它们始终位于中心?
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// rect
let layer = CAShapeLayer()
let bounds = CGRect(x: 60, y: 200, width: 250, height: 250)
layer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 20, height: 20)).cgPath
layer.strokeColor = UIColor.white.cgColor
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 4
view.layer.addSublayer(layer)
let rectAnimation = CABasicAnimation(keyPath: "strokeEnd")
rectAnimation.fromValue = 0
rectAnimation.toValue = 1
rectAnimation.duration = 2
layer.add(rectAnimation, forKey: "line")
}
Masonry 是一个强大的约束工具,您会非常喜欢它。设置约束并为它们设置动画非常容易; 否则,您也可以将 x 和 y "bounds" 坐标设置为 centerX 和 centerY