Swift动态UISwitch按钮

最近项目要用到有动态效果的switc按钮,效果图如下:


switch.gif

这种效果需要结合帧动画实现,然后赶紧叫UI弄了一组图过来...

首先我们需要自定义一个View

并且把这个View弄成图中的圆角

class DynamicSwitch: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        // 圆角
        layer.cornerRadius = height / 2
        layer.masksToBounds = true
    }
}

实现帧动画

// 帧动画
for i in 1...22 {
    beginImageArray.append(UIImage(named: "switch \(i).png")!)

    let imageView = UIImageView(image: UIImage(named: "switch 1.png"))
    imageView.center = view.center
    imageView.animationImages = beginImageArray
    // 动画持续时间 每一帧0.025秒,总共22帧
    imageView.animationDuration = 0.025 * 22
    imageView.animationRepeatCount = 1
    // 动画结束后要把imageView的image属性设置成动画最后的一帧
    imageView.image = UIImage(named: "switch 22.png")
    imageView.startAnimating()
    view.addSubview(imageView)
}
image.png

这个时候就可以做出表情原地旋转的效果了


switch.gif

然后我们需要一个渐变的颜色

渐变色可以通过 CAGradientLayer 实现,代码如下:

// 渐变色
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [UIColor(red: 255 / 255.0, green: 195 / 255.0, blue: 113 / 255.0, alpha: 1).cgColor, UIColor(red: 255 / 255.0, green: 95 / 255.0, blue: 95 / 255.0, alpha: 1).cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 0)
view.layer.addSublayer(gradientLayer)

startPoint 和 endPoint 从(0, 0)到(1, 0)代表的是水平方向的渐变色。
这时候我们的控件看起来长这样子:


switch.png

动画结合

整体的动画实际上就是imageView水平方向平移 + 帧动画,这时候我们通过一个bool类型来判断是从左到右还是从右到左:

// 点击的时候调用
func beginAnimation() {
    let duration = flag ? 0.0175
            * 21 : 0.0225 * 21
    customImageView.animationDuration = duration
    customImageView.animationRepeatCount = 1
    isEnabled = false
    customImageView.animationImages = flag ? beginImageArray : endImageArray
    customImageView.image = flag ? UIImage(named: "switch 22.png") : UIImage(named: "switch 1.png")
    UIView.animate(withDuration: duration, animations: { [unowned self] in
        self.customImageView.minX = self.flag ? self.width - self.height : 0
    }) { [unowned self] (_) in
        self.isEnabled = true
    }
    customImageView.startAnimating()
    flag = !flag
}
switch.gif

渐变色出现

最后一步,需要实现背景的渐变色跟着表情的移动一起出现。这时候可以通过一个大小跟父控件一样的白色view实现,点击时候x坐标跟表情同步。


whiteview.gif

结合帧动画


switch.gif

基本效果已经实现了,剩下的就是设置边框、阴影和一些细节上的调整。
Demo地址: https://github.com/JasonXJKJ/DynamicSwitch

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,816评论 25 709
  • 本文为性能优化系列的总纲,主要介绍性能调优专题计划、何为性能问题、性能调优方式及前面介绍的数据库优化、布局优化、J...
    雅然风懿阅读 607评论 0 5
  • 10.30减法策略 限定一个框架,在框架中有目的的寻找创新。
    张琪77阅读 234评论 0 0
  • (万尚学习会)打卡第55天 姓名:何炳 部门:业务部 组别:努力三组 【知~学习】 诵读《活法》第三章 拔"正剑"...
    CrisWellin阅读 164评论 0 0

友情链接更多精彩内容