Can you attach a UIGestureRecognizer to multiple views?

前端 未结 12 1028
忘了有多久
忘了有多久 2020-11-22 14:08
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];         


        
12条回答
  •  时光说笑
    2020-11-22 15:05

    What about re write (recreate) your GestureRecognize every time that you add a gesture recognizer pointing to the same func. In below case it works. I am using IBOutletCollection

    Swift 2:

    @IBOutlet var topicView: [UIView]!
    
    override func viewDidLoad() {
            for view in self.topicView as [UIView] {
            view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "viewClicked:"))
        }
    }
    
    func viewClicked(recognizer: UITapGestureRecognizer) {
        print("tap")
    }
    

提交回复
热议问题