需要动画的按钮
let customButton = UIButton.init(type: .custom)
customButton.backgroundColor = UIColor.blue
customButton.frame = CGRect.init(x: 180, y: 180, width: 50, height: 50)
customButton.setImage(UIImage.init(named: "Icon1"), for: .normal)
customButton.addTarget(self, action: #selector(customBtnClick(btn:)), for: .touchUpInside)
customButton.layer.cornerRadius = 25
customButton.clipsToBounds = true
view.addSubview(customButton)
点击动画效果
@objc func customBtnClick(btn: UIButton) {
if btn.isSelected {
btn.setImage(UIImage.init(named: "Icon1"), for: .normal)
}else {
btn.setImage(UIImage.init(named: "Icon2"), for: .normal)
}
let anima = CATransition()
anima.type = kCATransitionPush
if btn.isSelected {
anima.subtype = kCATransitionFromBottom
}else {
anima.subtype = kCATransitionFromTop
}
anima.duration = 0.5
btn.imageView?.layer.add(anima, forKey: "push")
btn.isSelected = !btn.isSelected
}
网友评论