我有UICollectionView
个按钮作为补充UICollectionReusableView
。当点击按钮时,我将UILabel
的子类实例作为子视图添加到集合视图中。标签实例有UIPanGestureRecognizer
和UIPinchGestureRecognizer
。由于集合视图,未调用标签的手势识别器。我的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")
}
}