いくつかのラベル(UILabel)を含むMyScrollView
というカスタムUIView/Scrollviewを作成しようとしています。これらのラベルは、選択。スーパービューに埋め込まれたカスタムUIViewでタップジェスチャー認識機能が受信されない
UILabelsのタップイベントの作業を行うために、私は彼らがすべてuserIteractionEnabled = true
を持っていることを確認し、私は以下のようにデリゲートを作成しました:
protocol MyScrollViewDelegate {
func labelClicked(recognizer: UITapGestureRecognizer)
}
私が作成したUIViewのはScrollViewController
に使用されているカスタム、このScrollViewController
が同様にデリゲートメソッドを実装:
import UIKit
import Neon
class ScrollViewController: UIViewController, MyScrollViewDelegate {
var curQuestion: IPQuestion?
var type: QuestionViewType?
var lastClickedLabelTag: Int = 0 //
init(type: QuestionViewType, question: IPQuestion) {
super.init(nibName: nil, bundle: nil)
self.curQuestion = question
self.type = type
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
view = MyScrollView(delegate: self, q: curQuestion!)
view.userInteractionEnabled = true
}
}
// implementations for MyScrollViewDelegate
extension ScrollViewController {
func labelTitleArray() -> [String]? {
print("labelTitleArray called in implemented delegate")
return ["Comments", "Answers"]
}
func labelClicked(recognizer: UITapGestureRecognizer) {
print("labelClicked called in implemented delegate")
let controller = parentViewController as? ParentViewController
controller?.labelClicked(recognizer)
lastClickedLabelTag = recognizer.view!.tag
}
}
// MARK: - handle parent's ViewController event
extension QuestionDetailViewController {
func updateActiveLabelsColor(index: Int) {
print("updating active labels color: \(index)")
if let view = view as? MyScrollView {
for label in (view.titleScroll.subviews[0].subviews as? [UILabel])! {
if label.tag == index {
label.transform = CGAffineTransformMakeScale(1.1,1.1)
label.textColor = UIColor.purpleColor()
}
else {
label.transform = CGAffineTransformMakeScale(1,1)
label.textColor = UIColor.blackColor()
}
}
}
}
}
これはScrollViewController
上には「親ビューコントローラへの子ビュー・コントローラとして、追加、および親の上部に配置されています見解:
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.view.backgroundColor = UIColor.whiteColor()
addChildViewController(scrollViewController) // added as a child view controller here
view.addSubview(scrollViewController.view) // here .view is MyScrollView
scrollViewController.view.userInteractionEnabled = true
scrollViewController.view.anchorToEdge(.Top, padding: 0, width: view.frame.size.width, height: 100)
}
アプリはビューにすべてを読み込むことができますが、タップジェスチャー/イベントは、カスタムMyScrollView
のラベルに渡されていません。このため、私はいくつかのGoogle検索を行い、アップルデベロッパーのWebサイトでEvent Delivery: Responder Chainと読んでヒットテストを行った。以下hitTest
機能はMyScrollView
でトリガすることができます。
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
print("hit test started, point: \(point), event: \(event)")
return self
}
hitTest
と私の観察はhitTest
機能があるだけときtouchesBegan()
とtouchesEnded()
方法がビューでトリガされていることです。 hitTest
がなければ、両方の関数がタップで呼び出されることはありません。
しかし、タックジェスチャーにはUILabel
が応答する運がありません。だからここで私は専門家に手を差し伸べています。助けてくれてありがとう!
親の関連するタッチメソッド内で、次のレスポンダに転送してみます。たとえば、親の中の 'touchesBegan'の中で、' self.nextResponder.touchesEnded'を呼び出すことを試みてください。 –
ラベルにuserInteractionEnabled = trueプロパティを設定しましたか?UILabelのデフォルト値はfalseなので、ジェスチャを受け取りたい場合はそれを変更する必要があります。 –
@VladimirK、はい、すべてのラベルで 'userIteractionEnabled'をtrueに設定しました。ありがとう – TonyGW