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
}
}
Related
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
}
}
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.
I need a UIButton to perform a specific action when the user touches inside the button, the without releasing the touch drag outside the button and then release the finger. I don't want the action to be performed if he touches inside the button and then drags out and without raising the finger goes back into the button again and then release finger (which would effectively become a touch up inside, which is also not the sender I am looking for). Is there any sender type to do the particular action in the specified condition? I hope the doubt is clear enough. Please mention if you have doubt regarding what I asked.
Is UIControlEventTouchUpOutside not working for you?
[myButton addTarget:self action:#selector(action) forControlEvents:UIControlEventTouchUpOutside];
you can do it as below
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *aTouch = [touches anyObject];
CGPoint point = [aTouch locationInView:self.view];
}
here what you need to do is that compare point (CGPoint) with your button's area and act accordingly
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.
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.