8.19 自定義控件實(shí)現(xiàn)功能

(Target-Action,協(xié)議,閉包)

簡(jiǎn)單控件用閉包

復(fù)雜控件用協(xié)議

新建一個(gè)類ProtocolSlider
//協(xié)議的優(yōu)點(diǎn):明確
//協(xié)議的缺點(diǎn):復(fù)雜,步驟太多
//如果有optional方法,必須使用@objc

@objc protocol ProtocolSliderDelegate {

    optional func didChange(slider: ProtocolSlider類名)

}

class ProtocolSlider: UIView {

    let trackView = UIView()      

    

//    用于關(guān)聯(lián)兩個(gè)對(duì)象(控件與需要獲取事件的對(duì)象)

    var delegate: ProtocolSliderDelegate!

//    如果使用閉包,定義一個(gè)閉包

//    var didChange: ((ProtocolSlider) -> Void)!

    var minValue: CGFloat = 0

    var maxValue: CGFloat = 1

    

    var currentValue: CGFloat = 0.5 {

        didSet {

            //            self.sendActionsForControlEvents(.ValueChanged)

            

            self.setNeedsLayout()

        }

    }

    

    override init(frame: CGRect) {

        super.init(frame: frame)

        

        self.addSubview(trackView)

        

        self.backgroundColor = UIColor.cyanColor()

        trackView.backgroundColor = UIColor.redColor()

    }

    

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        

        self.addSubview(trackView)

        

        self.backgroundColor = UIColor.cyanColor()

        trackView.backgroundColor = UIColor.redColor()

    }

    

    func moveTrack(touches: NSSet) {

        let touch = touches.anyObject() as! UITouch

        

        let location = touch.locationInView(self)

        currentValue = (location.x / self.frame.size.width) * (maxValue - minValue) +minValue

    }

    

    //開(kāi)始觸摸

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        //        print("begin")

        

        moveTrack(touches)

    }

    

    //移動(dòng)

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        //        print("continue")

        

        moveTrack(touches)

    }

    

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

        //        print("end")

        

        //觸發(fā)事件

//        self.sendActionsForControlEvents(.ValueChanged)

//        實(shí)現(xiàn)閉包

//        1. 如果使用協(xié)議,需要確保delegate是否為空

//        2. 如果方法為optional,需要確保方法是否實(shí)現(xiàn)

//        if delegate != nil {

//            if delegate.didChange != nil {

//                delegate.didChange!(self)

//            }

//        }

        

        if didChange != nil {

            didChange(self)

        }

    }

    

    override func layoutSubviews() {

        super.layoutSubviews()

        

        let width = (currentValue - minValue) * self.frame.size.width / (maxValue -minValue)

        let rect = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)

        trackView.frame = rect

    }

}

在ViewControl中實(shí)現(xiàn)協(xié)議

import UIKit

class ViewController: UIViewController, ProtocolSliderDelegate, UITableViewDelegate {

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //UIControl

        //Target-Action

        

        let slider = ProtocolSlider(frame: CGRect(x: 100, y: 100, width: 200, height:50))

//    UIControl    addTarget  實(shí)現(xiàn)方法   

//        slider.addTarget(self, action: #selector(didChange(_:)), forControlEvents: .ValueChanged)

//        slider.delegate = self

        

//        調(diào)用閉包

//        slider.didChange = {

//            sender in

//            print(sender.currentValue)

//        }

        

        //函數(shù):特殊閉包

        //閉包:匿名函數(shù)

        slider.didChange = didChange          協(xié)議實(shí)現(xiàn)方法   

        self.view.addSubview(slider)          協(xié)議實(shí)現(xiàn)方法   

    }

    func didChange(sender: ProtocolSlider) {
        print(sender.currentValue)
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,789評(píng)論 25 709
  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,582評(píng)論 30 472
  • 1、隨機(jī)數(shù) 不需要隨機(jī)數(shù)種子 arc4random()%N + begin:產(chǎn)生begin~begin+N的隨機(jī)數(shù)...
    我是小胡胡123閱讀 4,406評(píng)論 0 2
  • 因?yàn)橐Y(jié)局swift3.0中引用snapKit的問(wèn)題,看到一篇介紹Xcode8,swift3變化的文章,覺(jué)得很詳細(xì)...
    uniapp閱讀 4,852評(píng)論 0 12
  • 1 背景## 在Android中任何耗時(shí)的操作都不能放在UI主線程中,所以耗時(shí)的操作都需要使用異步實(shí)現(xiàn)。同樣的,在...
    我是昵稱閱讀 1,336評(píng)論 0 3

友情鏈接更多精彩內(nèi)容