我正在尝试制作一个带有聊天“泡泡”的应用程序,我需要在每个泡泡状态之间设置一个转换动画。
extension UIView {
func makeRoundedCorners(topLeftRad: Double, topRightRad: Double, bottomRightRad: Double, bottomLeftRad: Double) {
let path = UIBezierPath(roundedRect: self.bounds, topLeftRadius: CGFloat(topLeftRad), topRightRadius: CGFloat(topRightRad), bottomRightRadius: CGFloat(bottomRightRad), bottomLeftRadius: CGFloat(bottomLeftRad))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
let animation = CABasicAnimation(keyPath: "path")
animation.toValue = path.cgPath
animation.duration = 1
maskLayer.add(animation, forKey: "makeRoundedCorners")
self.layer.mask = maskLayer
}
}
extension UIBezierPath {
convenience init(roundedRect rect: CGRect, topLeftRadius r1: CGFloat, topRightRadius r2: CGFloat, bottomRightRadius r3: CGFloat, bottomLeftRadius r4: CGFloat) {
let left = CGFloat(Double.pi)
let up = CGFloat(1.5*Double.pi)
let down = CGFloat(Double.pi / 2)
let right = CGFloat(0.0)
self.init()
addArc(withCenter: CGPoint(x: rect.minX + r1, y: rect.minY + r1), radius: r1, startAngle: left, endAngle: up, clockwise: true)
addArc(withCenter: CGPoint(x: rect.maxX - r2, y: rect.minY + r2), radius: r2, startAngle: up, endAngle: right, clockwise: true)
addArc(withCenter: CGPoint(x: rect.maxX - r3, y: rect.maxY - r3), radius: r3, startAngle: right, endAngle: down, clockwise: true)
addArc(withCenter: CGPoint(x: rect.minX + r4, y: rect.maxY - r4), radius: r4, startAngle: down, endAngle: left, clockwise: true)
close()
}
}
所以我编写了一个自定义的UIBeziePath初始值设定项,然后如前所述为UIView添加了一个扩展。
但当我试图更新细胞的状态时,什么也没发生,只是立刻画出路径。我该怎么办?
我附上了一些照片想知道发生了什么事
maskLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: 5).cgPath
但现在这件事发生了