iOS adding tapGesture to multiple Views

前端 未结 6 1013
无人及你
无人及你 2020-12-31 03:11

I have multiple views defined in my main view. I want to add single tap gesture to all these views. Below is the code I have written, but this registers a tap gesture to the

6条回答
  •  执念已碎
    2020-12-31 03:33

    I had the same problem where it only added to the last view. There might be a better solution, but I just created a tag gesture for each view and linked them to the same selector method (oneTap: in your case). In order to distinguish which view activated the method, you can just tag your views, feedsView.tag = 0; peopleView.tag = 1; and so on. Then when the method is called:

    - (void)oneTap:(UIGestureRecognizer *)gesture {
        int myViewTag = gesture.view.tag;  // now you know which view called
        // use myViewTag to specify individual actions;
    }
    

提交回复
热议问题