美文网首页
swift 点击button将设置的图片进行转场动画替换(类似淘

swift 点击button将设置的图片进行转场动画替换(类似淘

作者: kayling | 来源:发表于2021-04-16 15:05 被阅读0次

需要动画的按钮

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
    }

相关文章

网友评论

      本文标题:swift 点击button将设置的图片进行转场动画替换(类似淘

      本文链接:https://www.haomeiwen.com/subject/jfeelltx.html