How to make 'touchesBegan' method work for a specific view?
Asked Answered
S

7

9

In my add contact page, i have a view and a scrollview on it and again a view on it. and in that last view i ve textboxes, etc. i have given the 'touchesBegan' method but it is called only for the view at the bottom. how to point that method to another view i.e., the view at the top?

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self.AddView endEditing:YES];
}   
Selfexecuting answered 4/7, 2013 at 10:26 Comment(0)
S
30

One way this is how you can do :

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

}

Please Note : As you are using UIScrollView you might not able to get touches method for UIScrollView. In that case you might have to use UIGesture.

Sewel answered 4/7, 2013 at 10:30 Comment(2)
thumbs up for mentioning it won't work with UIScrollView :)Ton
Yes, this is like going one step deeper to the problem :)Inamorata
J
9

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
    }
}
Juliannjulianna answered 7/6, 2015 at 23:15 Comment(2)
what does mean touches.firstIsla
@iOSDeveloper It is where the user first started touching the screen. There could be multiple of these touch events as the user could be dragging their finger on the screen. The last one would be touches.last.Juliannjulianna
R
6

First check the property UserInteractionEnabled of that whole controls and set to YES

After check out your bottom view frame that its not over on that views

And after you can checkout that with bellow condition and do something with particular controls touch event..

-(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
       }
     }
}
Report answered 4/7, 2013 at 10:44 Comment(0)
A
4

try this :

  - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch1 = [touches anyObject];
        CGPoint touchLocation = [touch1 locationInView:self.finalScore];

         if(CGRectContainsPoint(YourView.frame, touchLocation));
         {
            //Do stuff.
         }


  }
Aconcagua answered 4/7, 2013 at 10:32 Comment(0)
A
2

try

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:vTouch] anyObject];
    if(!touch)
        return;

    CGPoint pointNow = [touch locationInView:otherView];
    {
        // code here
    }
}
Antakiya answered 4/7, 2013 at 10:29 Comment(0)
T
1

If your view is a parent view then can 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.


        }
    }
Toot answered 8/9, 2019 at 19:24 Comment(0)
R
0

Referring to the Apple documentation on UIResponder, all UIView objects (which includes UIWindow), UIApplication object, UIViewController objects are all instances of UIResponder. To handle a specific type of event, a responder must override the corresponding methods.

In our case touches is our type of event. So our responder should implement the following methods. touchesBegan(:with:), touchesMoved(:with:), touchesEnded(:with:), and touchesCancelled(:with:)

Since we only wish to know when a user has touched a particular view, we only need to implement touchesBegan(_:with:). Since we are not overriding the other methods we must call super.touchesBegan(touches, with: event). If we were overriding ALL of the other methods, we wouldn't be required to call super.

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

For touches in a view, this set contains only one touch by default. Hence touches.first is the only UITouch instance in the set. We then access the property view, this represents the view or window in which the touch occurred. Lastly we compare the view that has been touched with your desired view.

It should be noted that if you wish to receive multiple touches- you must set the view's isMultipleTouchEnabled property to true. Then the set of touches will have more than one UITouch instance and you will have to handle that 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
        }
    }
Reforest answered 4/1, 2019 at 1:32 Comment(2)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Wrestling
@DangNguyen thanks for the suggestion. I've gone ahead and given a more detailed answer.Reforest

© 2022 - 2024 — McMap. All rights reserved.