美文网首页
Swift 面向协议的视图

Swift 面向协议的视图

作者: 闻道刘 | 来源:发表于2016-10-13 15:58 被阅读83次

情景假设

假设你有个非常简单的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/

相关文章

网友评论

      本文标题:Swift 面向协议的视图

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