美文网首页
iOS-一步一步了解Swift的面向协议编程

iOS-一步一步了解Swift的面向协议编程

作者: Simple_Code | 来源:发表于2018-08-23 18:01 被阅读32次

Objective-C协议:

OC中protocol的作用仅仅就是声明方法,然后交给任何类去实现

当我们想为一个协议提供默认实现时,通常会提供一个基类,基类遵从协议并且提供默认实现。如果不想使用默认实现,需继承基类,在子类中重写方法

Swift协议:

Swift在定义协议 protocol 时,可以使用 extension 给它的某些方法提供默认实现:

//protocol
protocol aProtocol{}
extension aProtocol where Self:UIView{
    //只给遵守myProtocol协议的UIView添加了拓展
    //强大的协议拓展  可以给协议添加默认实现   面向协议编程的基础
    func getString() -> String{
        return "string"
    }
    
}
class MyView:UIView{}
extension MyView:aProtocol{}
let myView = MyView()
let aStr = myView.getString()

下面我们通过一个例子、介绍下面向协议编程的好处:

项目需求:

需求1:点击button的时候给imageView加一个抖动效果
代码实现:
自定义ShakeImageView,添加抖动的方法

import UIKit

class ShakeImageView: UIImageView {

    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x - 10, y: self.center.y))
        animation.toValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x + 10, y: self.center.y))
        layer.add(animation, forKey: "position")
    }
}

调用:

    func onClick() {
        self.shakeImageView?.shake();
    }

需求2:imageView抖动的时候button也一起抖动
自定义ShakeButton,添加抖动的方法

import UIKit

class ShakeButton: UIButton {

    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x - 10, y: self.center.y))
        animation.toValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x + 10, y: self.center.y))
        layer.add(animation, forKey: "position")
    }

}

调用:

    func onClick() {
        self.shakeImageView?.shake();
        self.shakeButton?.shake();
    }

作为一个开发人员、你会觉得上面的代码很土,违背了三大特性封装的原则
下来我们开始优化、按照Objective-C的思想、我们会给UIView添加一个分类、在 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.init(cgPoint: CGPoint.init(x: self.center.x - 10, y: self.center.y))
        animation.toValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x + 10, y: self.center.y))
        layer.add(animation, forKey: "position")
    }
}

调用:


class ShakeImageView: UIImageView {
    // other customization here
}
 
class ShakeButton: UIButton {
    // other customization here
}
 
class ViewController: UIViewController {
    func onClick() {
        self.hakeImageView.shake()
        self.shakeButton.shake()
    }

这样是很好的解决了我们的问题,但是这样是给所有的继承UIView的控件添加的抖动属性,然后又有一个其他功能我们继续添加在分类里面、时间长了造成代码混乱、不易管理。

这里我们讨论的是Swift的面向协议编程,我们当然会用到协议。让我们创建一个 Shakeable 的协议:
import UIKit

protocol Shakeable {}

extension Shakeable where Self: UIView {
    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x - 10, y: self.center.y))
        animation.toValue = NSValue.init(cgPoint: CGPoint.init(x: self.center.x + 10, y: self.center.y))
        layer.add(animation, forKey: "position")
    }
}

如果我们需要某个继承UIView的控件发生抖动、我们只需要遵循该协议

import UIKit

class ShakeImageView: UIImageView ,Shakeable{
}

如果需要添加其他的功能、我们只需要添加其协议

个人感觉这种面试协议编程、解决了Objective-C不能进行多继承的问题。。。可以通过遵循多个协议代替多继承

这种思想也可以解下面问题:

给 UIViewController 添加 一个数据为空视图
给 UIViewController 添加 一个遮挡提示视图
给 xib 添加一个快速获取示例方法

参考文献:

参考1:https://blog.csdn.net/felicity294250051/article/details/79026969
参考2:https://www.jianshu.com/p/1546594b856b
参考3:https://blog.csdn.net/Mazy_ma/article/details/77942669

相关文章

网友评论

      本文标题:iOS-一步一步了解Swift的面向协议编程

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