How to make the "touchhesBegan" method work for a certain kind?

In my secondary contact page, I have a view and a scroll on it, and then a view of it. and in this last view i ve text fields etc. I gave the "touchhesBegan" method, but it is only called for presentation below. how to point this method to another view, i.e. view from above?

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.AddView endEditing:YES]; } 
+7
source share
7 answers

One way to do this:

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch= [touches anyObject]; if ([touch view] == image1) { //Action } } 

Please note:. Since you are using UIScrollView, you may not be able to get the touch method for UIScrollView. In this case, you may have to use UIGesture.

+27
source

Here is the answer in Swift:

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { let touch = touches.first as! UITouch if(touch.view == myView){ // Code } } 
+8
source

First check the UserInteractionEnabled property of all these controls and set YES

After you view the bottom view that will not be displayed in this view

And after you can verify that with a lower state and do something with specific touch controls.

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; [touch locationInView:viewBoard]; if([touch.view isKindOfClass:[UIImageView class]]) { UIImageView *tempImage=(UIImageView *) touch.view; if (tempImage.tag == yourImageTag) { /// write your code here } } } 
+6
source

try the following:

  - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch1 = [touches anyObject]; CGPoint touchLocation = [touch1 locationInView:self.finalScore]; if(CGRectContainsPoint(YourView.frame, touchLocation)); { //Do stuff. } } 
+4
source

to try

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event touchesForView:vTouch] anyObject]; if(!touch) return; CGPoint pointNow = [touch locationInView:otherView]; { // code here } } 
+1
source

Referring to Apple's UIResponder documentation , all UIView objects (including UIWindow), the UIApplication object, UIViewController objects are instances of the UIResponder. To handle a specific type of event, the respondent must override the appropriate methods.

In our case, touches is our type of event. Therefore, our respondent must implement the following methods. touchSegan (: c :), touchMoved (: c :), TouchEnded (: c :), and TouchCancelled (: c :)

Since we only want to know when the user has touched a certain view, we only need to implement touchesBegan(_:with:) . Since we do not override other methods, we must call super.touchesBegan(touches, with: event) . If we redefined ALL other methods, we would not have to call super .

Looking at touchesBegan(_:with:) the touches parameter of a collection of UITouch instances. Each instance represents touches for the initial phase of the event, which is represented by the event parameter.

For touches in a view, this set by default contains only one touch. Therefore, touches.first is the only UITouch instance in the collection. Then access to the real estate view , this is the view or window in which the touch occurred. Finally, we compare the view that has been affected with your desired view.

It should be noted that if you want to get several touches-, you must set the isMultipleTouchEnabled property of the isMultipleTouchEnabled view isMultipleTouchEnabled true . Then the touches will have more than one UITouch instance, and you will have to handle it accordingly.

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) if let touch = touches.first, touch.view == myView { // Do something } } 
0
source

If your view is parent, use this code:

 if let touch = touches.first { let position = touch.location(in: yourView) let pnt: CGPoint = CGPoint(x: position.x, y: position.y) if (yourView.bounds.contains(pnt)) { //You can use: yourView.frame.contains(pnt) //OK. } } 
0
source

Source: https://habr.com/ru/post/1489668/


All Articles