我想在点击视图时添加动画。虽然第一次显示它的圆,但当我转换视图大小更改。我想要添加动画,而更改背景动画节目就像从中心到视角的更改圆圈。 我的代码
class ClickableView: UIControl {
@IBInspectable public var selectedColor: UIColor = UIColor.clear{
didSet{
updateColor()
}
}
override open var isSelected: Bool{
didSet{
UIView.animate(withDuration: 0.2, animations: {
self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}) { (finished) in
UIView.animate(withDuration: 0.3, animations: {
self.transform = CGAffineTransform.identity
})
}
updateLayout()
updateColor()
}
}
@IBInspectable public var mainColor: UIColor = UIColor.white{
didSet{
updateColor()
}
}
// MARK: - initializers
override init(frame: CGRect) {
super.init(frame: frame)
addAction()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addAction()
}
func updateLayout(){
self.layer.cornerRadius = self.frame.size.width/2
self.layer.masksToBounds = true
self.layer.borderWidth = 10
self.layer.borderColor = UIColor.gray.cgColor
}
// MARK: - draw rect
override func draw(_ rect: CGRect) {
super.draw(rect)
updateLayout()
}
@objc func touchUpInside(){
self.isSelected = isSelected == true ? false:true
}
func addAction() {
self.addTarget(self, action: #selector(touchUpInside), for: .touchUpInside)
}
private func updateColor(){
UIView.animate(withDuration: 0.3) { [weak self] in
guard let strongSelf = self else{return}
strongSelf.backgroundColor = strongSelf.isSelected == true ? strongSelf.selectedColor:strongSelf.mainColor
}
}
}