【发布时间】:2020-06-10 16:34:25
【问题描述】:
我正在尝试创建一个 CAShapeLayer 动画,该动画在 UILabel 的框架周围绘制轮廓。代码如下:
func newQuestionOutline() -> CAShapeLayer {
let outlineShape = CAShapeLayer()
outlineShape.isHidden = false
let circularPath = UIBezierPath(roundedRect: questionLabel.frame, cornerRadius: 5)
outlineShape.path = circularPath.cgPath
outlineShape.fillColor = UIColor.clear.cgColor
outlineShape.strokeColor = UIColor.yellow.cgColor
outlineShape.lineWidth = 5
outlineShape.strokeEnd = 0
view.layer.addSublayer(outlineShape)
return outlineShape
}
func newQuestionAnimation() {
let outlineAnimation = CABasicAnimation(keyPath: "strokeEnd")
outlineAnimation.toValue = 1
outlineAnimation.duration = 5
newQuestionOutline().add(outlineAnimation, forKey: "key")
}
动画在 iPhone 11 的模拟器上运行时按预期执行,这是我在故事板中使用的设备尺寸。但是,当在具有不同屏幕尺寸的不同设备(如 iPhone 8 plus)上运行项目时,形状会被画出位置,而不是像应有的那样围绕 UILabel 绘制。我使用自动布局将 UILabel 水平和垂直居中到视图的中心,因此无论使用什么设备, UILabel 都会居中。
有什么建议吗?提前致谢!
干杯!
【问题讨论】:
标签: swift xcode swift4 swift5 cashapelayer