Swift 面向协议的视图

情景假设

假设你有个非常简单的app,里面只有一张图片和一个按钮。产品想要你实现在点击按钮的时候图片进行晃动。
要实现这个功能非常简单,网上也有很多代码。随便搜。

关键是这段代码放在什么位置比较合适。

当然我们可以选去新建一个图片的子类,在里面给它写一个晃动的动画。

//  FoodImageView.swift

import UIKit

class FoodImageView: UIImageView {
    
    // shake code goes here
    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y))
        animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y))
        layer.addAnimation(animation, forKey: "position")
    }
}

现在,只要用户一点button,我就调用图片的shake方法。

//  ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var foodImageView: FoodImageView!
    
    @IBAction func onShakeButtonTap(sender: AnyObject) {
        // shake method called here
        foodImageView.shake()
    }
}

拓展功能

然而,在日常开发中,你刚实现完这个功能,产品就跑过来说他们想把button也弄一个晃动的动画。。。= 。=

当然,你可以再来一个button自定义,给它写个shake方法。。。
但这样就不符合DRY原则了。

如果你是从oc转swift的话,面对这种问题,你肯定是先想到写个category(类别),swift里也可以写类别。

//  UIViewExtension.swift

import UIKit

extension UIView {
    
    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y))
        animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y))
        layer.addAnimation(animation, forKey: "position")
    }
}

这样一来,button和imageview都有一个shake的方法了。

class FoodImageView: UIImageView {
    // other customization here
}

class ActionButton: UIButton {
    // other customization here
}

class ViewController: UIViewController {

    @IBOutlet weak var foodImageView: FoodImageView!
    @IBOutlet weak var actionButton: ActionButton!
    
    @IBAction func onShakeButtonTap(sender: AnyObject) {
        foodImageView.shake()
        actionButton.shake()
    }
}

看起来很爽,很好用,是吗?
然而。。。这样写,如果其他开发者来看你的代码,或者接手你的工作,他们无法单从一个imageview和一个button就很快地知道它们有一个shake方法。你知道是因为这个category是你自己写的。
而且,类别多了之后,很容易就脱离控制了,整个项目慢慢会变得混乱,代码的可读性甚至会越来越差,你根本不知道为什么会有个方法在这里,什么时候用这个方法。

那,有什么更好的办法呢?

面向协议

对,就是它。swift里的解决方案就是利用协议。我们先利用协议延伸性来建一个shakeable协议,里面的默认的方法就是shake。

//  Shakeable.swift

import UIKit

protocol Shakeable { }

// we can constrain the shake method to only UIViews!
extension Shakeable where Self: UIView {
    
    // default shake implementation
    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y))
        animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y))
        layer.addAnimation(animation, forKey: "position")
    }
}

ok,现在我们只需要在那些想用动画的view上面遵循协议就好了。

class FoodImageView: UIImageView, Shakeable {
    // other customization here
}

class ActionButton: UIButton, Shakeable {
    // other customization here
}

class ViewController: UIViewController {

    @IBOutlet weak var foodImageView: FoodImageView!
    @IBOutlet weak var actionButton: ActionButton!
    
    @IBAction func onShakeButtonTap(sender: AnyObject) {
        foodImageView.shake()
        actionButton.shake()
    }
}

这样写出来的代码很明显就是可读性高了。只需要看看类的声明,马上就知道这里是要走一个shake方法,有晃动的动画。
以后产品再跑过来说让你在别的地方加上这个动画,你要做的只需要遵循这个协议就好了。非常简单!

总结

在view上面利用协议编程的思想,令你的代码更具可读性,复用性并且易于维护。

本文出自,内容非逐字翻译。
https://www.natashatherobot.com/protocol-oriented-views-in-swift/

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

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,773评论 4 61
  • 生日快乐 终究这个生日还是没有你的祝福
    Meggieeeet阅读 1,292评论 0 0
  • 现如今很多孩子都是爷爷奶奶带大的,因为老人宠孩子,其自理能力也每况愈下。而家长们似乎并不在意这些,在意的是孩子班里...
    蓝四阅读 2,047评论 1 3
  • 《当我谈跑步时我谈些什么》是我去年夏天在猫空买来的,当时朋友说这本书很适合我看,作为一个喜欢晨跑同时喜欢村上春树的...
    月亮酱阅读 6,454评论 3 13
  • 月光撒成思乡的网 把乡愁浓浓泼墨 路边野花迷乱了眼 山路蜿蜒每晚的梦 黝黑的臂膀抡起责任 黄土地养育辈辈辛劳 炊烟...
    金钗银环阅读 3,449评论 49 61

友情链接更多精彩内容