Reputation: 740
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];
}
Upvotes: 9
Views: 29897
Reputation: 850
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.
}
}
Upvotes: 1
Reputation: 1
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
}
}
Upvotes: 0
Reputation: 1024
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
}
}
Upvotes: 9
Reputation: 20541
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
}
}
}
Upvotes: 6
Reputation: 3510
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.
}
}
Upvotes: 4
Reputation: 4500
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.
Upvotes: 30
Reputation: 1791
try
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:vTouch] anyObject];
if(!touch)
return;
CGPoint pointNow = [touch locationInView:otherView];
{
// code here
}
}
Upvotes: 2