具有集合视图的手势识别器

时间:2015-05-21 04:05:23

标签: ios uicollectionview uilabel uigesturerecognizer uipangesturerecognizer

我有UICollectionView个按钮作为补充UICollectionReusableView。当点击按钮时,我将UILabel的子类实例作为子视图添加到集合视图中。标签实例有UIPanGestureRecognizerUIPinchGestureRecognizer。由于集合视图,未调用标签的手势识别器。我的UILabel子类如下:

import UIKit

public class SJLabel: UILabel {

    // The pinch associated with the label
    private var pinch: UIPinchGestureRecognizer!
    private(set) public var pan: UIPanGestureRecognizer!

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialSetUp()
    }

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

    private func initialSetUp() {
        numberOfLines = 0

        pinch = UIPinchGestureRecognizer(target: self, action: "handlePin:")
        addGestureRecognizer(pinch)

        pan = UIPanGestureRecognizer(target: self, action: "handlePan:")
        addGestureRecognizer(pan)
    }
}

extension SJLabel {

    @objc private func handlePan(recognizer: UIPanGestureRecognizer) {

        let location = recognizer.locationInView(superview)
        print(location)
    }

    @objc private func handlePin(recognizer: UIPinchGestureRecognizer) {
        if recognizer.numberOfTouches() != 2 {
            return
        }

        println("Handling")
    }
}

0 个答案:

没有答案