子类

子类化 UIControl 使我们能够访问以下方法:

  • 当手指首次触及控制范围内时,会调用 beginTrackingWithTouch
  • 当手指滑过控件甚至在控件的边界之外时,会反复调用 continueTrackingWithTouch
  • 当手指抬离屏幕时,会调用 endTrackingWithTouch

MyCustomControl.swift

import UIKit

// These are out self-defined rules for how we will communicate with other classes
protocol ViewControllerCommunicationDelegate: class {
    func myTrackingBegan()
    func myTrackingContinuing(location: CGPoint)
    func myTrackingEnded()
}

class MyCustomControl: UIControl {

    // whichever class wants to be notified of the touch events must set the delegate to itself
    weak var delegate: ViewControllerCommunicationDelegate?
    
    override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
        
        // notify the delegate (i.e. the view controller)
        delegate?.myTrackingBegan()
        
        // returning true means that future events (like continueTrackingWithTouch and endTrackingWithTouch) will continue to be fired
        return true
    }
    
    override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
        
        // get the touch location in our custom control's own coordinate system
        let point = touch.locationInView(self)
        
        // Update the delegate (i.e. the view controller) with the new coordinate point
        delegate?.myTrackingContinuing(point)
        
        // returning true means that future events will continue to be fired
        return true
    }
    
    override func endTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) {
        
        // notify the delegate (i.e. the view controller)
        delegate?.myTrackingEnded()
    }
}

ViewController.swift

这是视图控制器设置为委托并响应来自我们的自定义控件的触摸事件的方式。

import UIKit
class ViewController: UIViewController, ViewControllerCommunicationDelegate {
    
    @IBOutlet weak var myCustomControl: MyCustomControl!
    @IBOutlet weak var trackingBeganLabel: UILabel!
    @IBOutlet weak var trackingEndedLabel: UILabel!
    @IBOutlet weak var xLabel: UILabel!
    @IBOutlet weak var yLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myCustomControl.delegate = self
    }

    func myTrackingBegan() {
        trackingBeganLabel.text = "Tracking began"
    }
    
    func myTrackingContinuing(location: CGPoint) {
        xLabel.text = "x: \(location.x)"
        yLabel.text = "y: \(location.y)"
    }
    
    func myTrackingEnded() {
        trackingEndedLabel.text = "Tracking ended"
    }
}

笔记

  • 在没有子类化的情况下实现相同结果的替代方法包括添加目标或使用手势识别器。

  • 如果它们仅在自定义控件本身中使用,则不必使用具有这些方法的委托。我们本来可以添加一个 print 语句来显示事件是如何被调用的。在这种情况下,代码将简化为

      import UIKit
      class MyCustomControl: UIControl {
    
          override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
              print("Began tracking")
              return true
          }
    
          override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
              let point = touch.locationInView(self)
              print("x: \(point.x), y: \(point.y)")
              return true
          }
    
          override func endTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) {
              print("Ended tracking")
          }
      }