Swift EXC_BAD_ACCESS与自定义委托协议

时间:2016-04-22 17:55:18

标签: ios swift delegates

我正在尝试将UIPanGestureRecognizer子类化以添加touchesBegan方法。我打算使用委托模式来打开该方法,但每当我尝试引用traceDegate时,我都会收到错误的访问代码。

import UIKit
import UIKit.UIGestureRecognizerSubclass

protocol TracePanDelegate {
    func didTouchDown(recognizer: TracePanRecognizer)
}

class TracePanRecognizer: UIPanGestureRecognizer {
    var traceDelegate: TracePanDelegate?
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        traceDelegate?.didTouchDown(self)
    }
}

@IBDesignable class TraceSlider: UIView, TracePanDelegate {
    @IBOutlet var gestureRecognizer: TracePanRecognizer!

    @IBAction func didPan(gesture: TracePanRecognizer) {
        print("did pan")
    }
    var view: UIView!

    func didTouchDown(recognizer: TracePanRecognizer) {
        print("did touch down")
    }

    required override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    func xibSetup() {
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        addSubview(view);
        self.gestureRecognizer.traceDelegate = self //THROWS EXC_BAD_ACCESS 
        self.setSelectorRadius(self.view.frame.size.height/2);
    }
    func loadViewFromNib() -> UIView {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "TraceSlider", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        return view
    }
}

0 个答案:

没有答案