touchesBegan withEvent is not responding in uiscrollview - ios

What I had done in my xib file is that,
Initially I had an uiImageView and over that I had an UIScrollView on which i had put so many other controls.
Earlier I was not having scroll view so my touchesBegan event was working properly but now after putting UIScrollView its not responding.
I am new in iOS so please tell me what to do??
What I want to do is to hide my keyboard which appears for my UITextView, whenever I touch anywhere on screen.
So please help me out...
the code of my touchesBegan method is:
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([self.txtLoginId isFirstResponder] && [touch view] != self.txtLoginId)
{
[self.txtLoginId resignFirstResponder];
}
if ([self.txtPassword isFirstResponder] && [touch view] != self.txtPassword)
{
[self.txtPassword resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}

Probably, your scrollView has its property userInteractionEnabled set to YES. In this case, the scrollView receives the touches, and they will not be forwarded to your method touchesBegan:withEvent:.
Simply try to set this property to NO. This can be done in storyboard in the view section of the scrollView, or programmatically.

Related

Handling Touch Events Of A UIView That Contains A UIButton

I'm not able to capture touch events from a UIView with a UIButton subview. the UIButton probably prevents the touchesBegan and touchesEnd events from reaching the outer view.
How can I handle those events? I tried setting UserInteractionEnabled to false. The touch event reached the outer view but the UIButton does not transition to highlighted state with this solution.
Is there a way to accomplish this without losing the natural behavior of the button?
(P.S. I do not want to use GestureRecognizers etc. because I need to get the actual timestamp of the event)
If I have understood your question, you can try this approach:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if (CGRectContainsPoint(yourbutton.frame, [touch locationInView:self])) { //if touch is beginning on Button
}
}

Want Button to swallow all the touches

I am trying to build my own drop down list. I have created a custom view by creating subclass of UIButton. First time when button is clicked List will be displayed. Now I want that next time when it clicks anywhere outside that Button and list, this class should be notified. Or in other words I want this Button to swallow all the touches.
How could I achieve this?
Use touchesBegan method.
ex:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([touch View] != yourbutton)
{
//do what u want to do
}
}

iOS define touchable areas of an object, confine touches to subviews of self

I have a subclassed UIView I'll call customView. I would like enable touches so users can manipulate subviews, which have gesture recognizers and other controls, but the view itself I would like to not be touchable so that views drawn below the view will still be touchable. In other words customView will be drawn on top of other views in the app, but I still want the views below to be touchable, while allowing touches on subviews of customView.
I've tried to use touchesBegan like so but this does not work. Any ideas? Thanks for reading!
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
//I've tagged the views that I want to be touchable.
if ([touch view].tag == 1000 || [touch view].tag == 2000 || [touch view].tag == 3000) {
self.userInteractionEnabled = YES;
} else {
self.userInteractionEnabled = NO;
}
}
What you need to do is implement the following method in your customView:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
//check if one of the subviews was hit, if so forward the touch event to it
for (UIView *view in self.subviews){
if (CGRectContainsPoint(view.frame, point))
return view;
}
// use this to pass the 'touch' upward in case no subviews trigger the touch
return [super hitTest:point withEvent:event];
}
Then add those subview upper to your customview.other vice your customview should not let them touche.and put custom view to below those subview so none other area then your selected subview will be touchable.

Touches method in UIViewController and UIView

I'm working on iPad app. My UIViewController contains only a custom UIView with a size of 500wx500h.
I implemented the touches methods both in the UIViewController and the custom UIView in order to call the UIViewController touches methods when we touch all around the custom UIView and call the custom UIView touches methods when we touch inside it.
UIViewController touchesMoved :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(#"TEST"); // Added during edition
if (!CGRectContainsPoint(self.drawingView.frame, point)) {
NSLog(#"UIVIEWCONTROLLER");
}
}
Custom UIView touches Moved :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"CUSTOM VIEW");
}
When I'm touching the custom UIView moving my finger, CUSTOM VIEW is logged until I removed the finger on the screen. On the other hand, when I'm touching outside of the custom UIView, the UIVIEWCONTROLLER is called only one time in the touchesMoved method.
Am I missing something wrong ?
EDIT : I added a log in my UIViewController touchesMoved method. When I touch inside the custom view, it logs the TEST during all the touching phase. But when I touch outside, I get the same behaviour.
add exclusiveTouch property to your view to disable multi touches
your view.exclusiveTouch=YES;
There is some likeliness that your self.view is intercepting touches and handling them, so they will not make it through to the view controller. You could try doing either of 2 things (or both) and see if it works:
self.view.exclusiveTouch = NO;
self.view.userInteractionEnabled = NO;
I would also try to call [super touchesMoved: touches withEvent: event];
Hope it helps.

UITextField: force resignFirstResponder when tapped outside of it

I have a static UITableView that contains a number of UITextFields. When the user taps outside of any of the text fields I'd like to I'd like to dismiss the keyboard.
At the top level of my UIViewController is a UITableView and a UITabBarItem. I believe I'll also have to handle taps on the status bar.
I am unsure as how I should register for touches on them (so that I can force any of the text fields to call resignFirstResponder.
I thought I might have to handle UIResponder's
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
for the table view and tab bar item but neither of them are UIControls so I can't use
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
UIWindow is also a UIResponder but again, I can't seem to get touch events for it.
Any help would be much appreciated,
CS
If you had one text field I'd add touches began into the UIViewController it belongs to and do it like this...
- (void)touchesBegan ... cant remember full name
{
if ([touches count] > 1) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView self.view];
if (!CGRectContainsPoint(self.textField.frame, touchPoint)) {
//touch originated outside textField.
[textField resignFirstResponder];
}
}
If you have more than one text field then just do the CGRectContainsPoint check for each of them inside the if.
When you have a static UITableView it "eats" the touch events. So what I did was by subclassing UITableView and then added a delegate which reports touch events.

Resources