使用戰平的UIView屬性我有這樣我的自定義視圖:動畫了在矩形
@IBDesignable
class MyTableViewCell: UITableViewCell {
@IBInspectable var cardColor : UIColor = UIColor.white
@IBInspectable var cardHorizontalPadding : Int = 20
@IBInspectable var cardVerticalPadding : Int = 20
@IBInspectable var cardCornerRadius : Int = 10
@IBInspectable var cardShadowRadius : Int = 6
var cardLayer : CAShapeLayer = CAShapeLayer()
var cardRect = CGRect.zero;
override func awakeFromNib() {
super.awakeFromNib()
}
override func draw(_ rect: CGRect) {
self.backgroundColor = UIColor.clear
print("Sublayers count is \(layer.sublayers?.count)")
let cardRect = rect.insetBy(dx: CGFloat(self.cardHorizontalPadding), dy: CGFloat(self.cardVerticalPadding))
let cardPath = UIBezierPath(roundedRect: cardRect, cornerRadius: CGFloat(self.cardCornerRadius)).cgPath
cardLayer.lineWidth = 0
cardLayer.fillColor = self.cardColor.cgColor
cardLayer.path = cardPath
cardLayer.shadowRadius = CGFloat(self.cardShadowRadius)
cardLayer.shadowOffset = CGSize.init(width: -0.2, height: -0.2)
cardLayer.shadowPath = cardPath
cardLayer.shadowOpacity = 0.2
layer.addSublayer(cardLayer)
layer.replaceSublayer(cardLayer, with: cardLayer)
}
override func setSelected(_ selected: Bool, animated: Bool) {
// Configure the view for the selected state
}
}
我想動畫在cardVerticalPadding
,這樣
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/10, animations: {
currentCell.cardVerticalPadding = 10
})
,它是行不通的。