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

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

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

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,131评论 3 119
  • 这几日天气慢慢早晚感觉凉意了,可是中午的还是温度很高,但不似夏日的闷热,躲在阴凉的地方还是能感觉到秋的舒适!可是云...
    绿塬阅读 455评论 0 2
  • 记得那年16,拖着脑袋斜靠在桌上,想着读书有什么用处。。。 那时的自己是怎么想也想不明白。 父母,爷爷,奶奶,同学...
    厚积薄发阅读 603评论 0 2
  • 最近读书,关于block的底层实现,有以下思考和总结 c++部分的相关步骤分析写在代码注释 一、block是什么 ...
    GemShi阅读 710评论 0 12
  • 大二后,学校各组织都积极的开始活动着,才发现除了上学以外,同学们具备才能的人可不在少数。对于我这种各种都不突出的人...
    闲_昆明阅读 306评论 0 0

友情链接更多精彩内容