上面的这个『可插拔的Mixin』是我自己取的名字。其主要代指的是这种情况:常常我们需要给一个类添加一个通用性的功能,例如给一个有网络访问的ViewController添加一个进度条或者是加一个Loading的标志等。这时候如果逐一在各个类里面实现,会在调试和维护的时候产生很多麻烦。而如果通过类继承的方法来做,则限制其使用范围。下面这种Mixin的方法是我之前在做Java的时候收到启发想出来的,也许现在也有其他人已经做过了吧。
我通过下面这个给ViewController添加loading标识的例子来说明这个设计思路,其核心是利用Protocol的方法的Default Implementation来给ViewController添加新的功能:
实现Mixin的插件:
protocol LoadingMixin: class {
/**
调用这个函数来显示Loading标识
*/
func lm_start()
/**
调用这个函数来隐藏Loading标识
*/
func lm_stop()
var lm_container: UIView! { get }
var lm_loadingView: UIView! { get set }
var lm_timer: UIView! { get set }
var lm_delayTask: dispatch_block_t? { get set }
}
private var containerHandle: UInt8 = 1
private var loadingViewHandle: UInt8 = 2
private var timerHandle: UInt8 = 3
private var taskHandle: UInt8 = 4
// MARK: - 在extension里面我们可以给protocol添加默认实现
extension LoadingMixin where Self: UIViewController {
var lm_container: UIView! {
get {
if let container = objc_getAssociatedObject(self, &containerHandle) as? UIView {
return container
} else {
let container = UIView()
// TODO: 在这里创建LoadingView的样式
return container
}
}
}
var lm_loadingView: UIImageView! {
set {
objc_setAssociatedObject(self, &loadingViewHandle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
// 确保 LoadingViwe被创建了
self.lp_container
return objc_getAssociatedObject(self, &loadingViewHandle) as? UIImageView
}
}
var lm_timer: NSTimer! {
set {
objc_setAssociatedObject(self, &timerHandle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, &timerHandle) as? NSTimer
}
}
var lm_delayTask: dispatch_block_t? {
set {
objc_setAssociatedObject(self, &taskHandle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, &taskHandle) as? NSTimer
}
}
func lp_stop() {
if lm_timer != nil {
lm_timer.invalidate()
lm_timer = nil
lm_container.hidden = true
lm_container.removeFromSuperview()
} else if self.delayTask != nil {
dispatch_block_cancel(lm_delayTask!)
} else {
assertionFailure()
}
delayTask = nil
}
func lp_start() {
// kLoadingAppearDelay是一个延时常量,需要你自己在外部定义,或者直接替换成一个常数
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_MSEC) * kLoadingAppearDelay)
lm_timer = nil
lm_delayTask = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, { [weak self] in
guard let sSelf = self else {
return
}
let timer = NSTimer.schedule(repeatInterval: 0.05, handler: { (_) in
// 你可以重写这个部分来自定义Loading动画
let curTrans = sSelf.lm_loadingView.transform
let newTrans = CGAffineTransformRotate(curTrans, 0.4)
sSelf.lm_loadingView.transform = newTrans
})
sSelf.lm_timer = timer
})
dispatch_after(delay, dispatch_get_main_queue(), lm_delayTask!)
}
}
在上面的代码中,我们定义了一个名为LoadingMixin
的协议,并且extension中实现其涉及的主要方法:
-
lm_start
: 最终供viewController使用的接口,调用这个函数来显示Loading标识; -
lm_stop
: 也是供viewController使用的接口,调用这个函数来隐藏Loading标识; -
lm_container
: Loading标志的view hierarchy的顶层view; -
lm_loadingView
: Loading标志中需要动画旋转的图; -
lm_timer
: 驱动动画的timer; -
lm_delayTask
: 涉及这个的功能我们在下面解释。
绝大多数的情况下你不用重写上面的属性或者方法。
进一步的代码解释:
在上面的代码中,有两个问题还需要解释一下:
一是我在lm_start
中采用了一种延时启动的方式,当lm_start
被调用以后,并不会马上显示LoadingView,显示的代码被封装在一个block中,定在kLoadingAppearDelay
毫秒之后执行。而如果在这之前lm_stop
被调用的话,其调用会被取消。在当网络环境比较好,请求在kLoadingAppearDelay
时间之内完成时,不需要显示Loading画面。
二是下面这部分
let timer = NSTimer.schedule(repeatInterval: 0.05, handler: { (_) in
let curTrans = sSelf.lm_loadingView.transform
let newTrans = CGAffineTransformRotate(curTrans, 0.4)
sSelf.lm_loadingView.transform = newTrans
})
使用如下定义的extension:
extension NSTimer {
/**
Creates and schedules a one-time NSTimer instance.
- Parameters:
- delay: The delay before execution.
- handler: A closure to execute after delay.
- Returns: The newly-created `NSTimer` instance.
*/
class func schedule(delay delay: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
let fireDate = delay + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, 0, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
return timer
}
/**
Creates and schedules a repeating NSTimer instance.
- Parameters:
- repeatInterval: The interval (in seconds) between each execution of
handler. Note that individual calls may be delayed; subsequent calls
to handler will be based on the time the timer was created.
- handler: A closure to execute at each repeatInterval.
- Returns: The newly-created NSTimer instance.
*/
class func schedule(repeatInterval interval: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
let fireDate = interval + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, interval, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
return timer
}
}
使用上面的Mixin:
如下例子:
class MyViewController: UIViewController, LoadingMixin {
func request() {
lm_start()
send_request(..., onFinish: {
...
self.lm_stop()
})
}
}
上面只是一个例子,大家可以用类似的思路来实现自己的『可插拔的Mixin』