TouchesEnded Not Getting Called 2nd Time - ios

My problem is as follows:
I'm displaying a message view with an attachment, in a standard view controller. When the user presses and holds the attachment icon it show the image on screen, when the user lets go the image disappears. This is to aid detecting screenshots while the user is viewing the image.
I use a long press gesture recognizer to detect the touch and then touchesEnded or touchesCancelled to detect the release of the touch.
My problem occurs when the user presses the screen with a second finger, as the release of the second touch is not reported. The code is below, the methods get called in this order:
First long press -> attachmentLongPressed called
Second long press -> attachmentLongPressed called
Release first finger -> touchesEnded called
Release second finger -> nothing called
-(void)attachmentImageLongPressed:(UIImageView *)sender{
if(!self.isAttachmentOpen){
[self setAttachmentOpen:YES];
// Show image...
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self setAttachmentOpen:NO];
// Remove image from view
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.isAttachmentOpen){
[self screenshotDetected];
}
}
The result is that the image view is left on screen with no way to dismiss it. Anybody have any suggestions?

I think it should be called touchesEnded when release the second finger. You can log all the touches from all callback to find out which method is being called,
However, it's possible that long pressed gesture might delays the touch ended event, so try to set delaysTouchesEnded to FALSE.
gestureLongPressed.delaysTouchesEnded = FALSE

Related

UIButton drag finger into the bounds

I'm trying to get a particular event to work.
The user should touch one button (A), this button would pop-up some UIView, in which there is another button (B).
If the user touchUpInside button A, the popup should disappear, and the Button (B) would not be clickable.
However, if the user clicks on button (A), and then drag his finger into the button (B) then this button (B) will be selected, but only fire if the user TouchUpInside this button (B)
I have tried the most obvious TouchDragEnter and TouchDragInside, but it does not do what I expected, you have to touchDownInside first for it to work. Since the TouchDownInside event has been done on the button (A), it can't be done on the button (B)
Have I missed something, or should I just go ahead a create a subClass of my own for this particular behaviour ?
To add some difficulty, the button (A) and button (B) are not on the same UIView.
If I was to go about this, I would ignore touchUpInside etc altogether and just focus on the drag event. In the touchesMoved method, keep checking where the touches are, and if they intersect the rect of the view you are concerned about.
Something like this : (apologies for incomplete example)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
if ( CGRectContainsPoint(imageView.bounds, locationInView) ) {
// Point lies inside the bounds...
I have just created a library called PopMenu that implements exactly the function you mentioned. The idea is it pops out buttons when you touchdown on the menu button and keep tracking the user's finger until the user touches up on the screen. It returns the button that the user ended his touch on.

Keep calling function as long as finger is pressed down

Right now I'm using:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Function called");
}
which activates on press down, which is correct. But, I also want to continue calling the function, let's say every second, as long as the finger is still on the screen (in other words: until the finger is lifted up).
How would I go about doing this?

UINavigationController and Touch Events in Left / Left-Bottom Corner of Screen

I'm trying to call action with UIControlEventTouchDown event for simple UIButton which placed in left-bottom corner of UIViewController (which pushed with UINavigationController).
I created storyboard to push view controller with button.
And added actions for button to trace touch events.
- (IBAction)touchUpInside:(id)sender {
NSLog(#"touchUpInside");
}
- (IBAction)touchDown:(id)sender {
NSLog(#"touchDown");
}
And also added touchesBegan to trace if it is called.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
NSLog(#"touchesBegan");
}
Now, with such setup, I have strange behaviour. There are some touch areas in left (width =13) and left-bottom (width = 50, height = 50) which respond differently on touches. If you will will make touch over those areas -touchesBegan is not called on touch down, as would be with normal behaviour. But it will be called only after touch up.
I believe left area is used in UINavigationControoler for interactive pop of pushed UIViewController. So two questions here.
For which functionality is responsible area in bottom-left?
How and where can I change behaviour to pass touch event to UIButton (for example if I want UIButton to respond on long touch event, when I pressing in "red" area)?
I had this same problem, and I fixed it by disabling the "swipe to go back" (technically called "interactive pop gesture" in UIKit) feature introduced in iOS 7.
Sample code to disable interactive pop gesture:
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
I believe this is due to the interactive pop gesture recognizer consuming/delaying touch events near the left edge of the screen (because a swipe to go back starts from the left edge) and thus causing the touch events to not be delivered to controls that are situated near the left edge of the view.

Modal dialog with buttons failing

I have layer, on which I placed sprite(yellow) and child menu("Main Menu: button), to look like modal dialog. There are also number of buttons, menus below that layer. What I want to achieve is whenever this layer is presented, do not propagate touch event to other items. This is what I've done in layer class:
- (id)init {
...
// I was hoping that this will take touch and eat, so if I tap on button it will not be propagated further, because it has lowest priority
[_menu setHandlerPriority:INT_MIN+1];
...
}
-(void)registerWithTouchDispatcher {
// With this I was hoping that if I tap anywhere else my layer class will capture it first
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:INT_MIN+2 swallowsTouches:YES];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// so I simply drop any tap on my layer
return NO;
}
Probably I'm missing something, but with this buttons below my layer are still getting the touch. So the modal concept doesn't work.
How can I make all child items of my yellow sprite touchable and the rest not touchable.
Your return in ccTouchBegan is the issue.
Your layer will only swallow touches when ccTouchBegan tells it to (returns YES).
Normally you would check if the touch is within the bounds of the layer and then return YES, but in this case, returning YES always will swallow all touches (unless there is another layer of priority INT_MIN, or INT_MIN+1).
Edit: And point 2 - make sure you're enabling touches to begin with:
[self setTouchEnabled:YES];

Why are UIViewController touchesBegan, touchesMoved & touchesEnded only being called when the first of two touches begins, moves or ends?

I'm having an issue with handling more than one touch through the touchesBegan/Moved/Ended methods of UIViewController. I'm also seeing the same behaviour in a cocos2d app (using ccTouchesBegan/Moved/Ended) so I think this question can be applied to all touch handling in iOS. I've put the code that I'm using below, followed by the results that I'm seeing.
All methods are implemented on a UIViewController subclass.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Began");
[self logTouchesFor: event];
[super touchesEnded: touches withEvent: event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Moved");
[self logTouchesFor: event];
[super touchesEnded: touches withEvent: event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Ended");
[self logTouchesFor: event];
[super touchesEnded: touches withEvent: event];
}
-(void)logTouchesFor:(UIEvent *)event
{
int count = 1;
for (UITouch *touch in event.allTouches)
{
CGPoint location = [touch locationInView: self.view];
NSLog(#"%d: (%.0f, %.0f)", count, location.x, location.y);
count++;
}
}
Now for the interesting results...
Single Touches Work as Expected
Let's say I touch the screen with my thumb. I see in the output window that touchesBegan has been called as expected. I move my thumb around and touchesMoved gets called. Then I lift my thumb off the screen and touchesEnded gets called. All this is as expected, I'm including it in the question as a control case - just to be clear that my view controller is receiving touch events and I haven't missed a vc.view.userInteractionEnabled = YES anywhere.
The Second Touch Doesn't Cause touchesBegan, touchesMoved or touchesEnded to be called
This is the most interesting one. Let's say I touch the screen with my thumb (touchesBegan is called) and hold it still on the screen. Then I touch somewhere else on the screen with my index finger, whilst keeping my thumb in the same place. TouchesBegan is not called. Then let's say I move my index finger whilst keeping my thumb absolutely still (this can be tricky but it is possible). TouchesMoved is not called. Then, I lift my index finger off the screen. TouchesEnded is not called. Finally, I move my thumb and touchesMoved is called. Then I lift my thumb from the screen and touchesEnded is called.
Just to be clear: I've set self.view.multipleTouchEnabled = YES in my viewDidLoad method.
Information About the Second Touch is Available, Providing the First Touch Moves
This time I do something very similar to the example immediately above. I touch the screen with my thumb, then index finger whilst keeping my thumb still. TouchesBegan is called when my thumb hits the screen, but not my index finger. Now I move my thumb, and touchesMoved is called. Not only that, but there are two touches in the event.allTouches array (and yes, the second touch is where I would expect it to be). This means that the system is aware that I have touched the screen a second time, but I am not being notified through the touch handling methods on my view controller.
How Can I be Notified About Changes to the Second Touch?
I'd really like to be able to respond to changes in the location or state of the second touch as they happen, rather than when the first touch also changes. A lot of the time this won't be an issue because it's very hard to change one touch without impacting on the other, but I have at least one situation where it can be an issue. Am I missing something obvious? Has anyone else noticed this behaviour or had issues with it?
In case it's relevant, I'm using an iPhone 3GS running iOS 5.1.
Ok, I started painstakingly rebuilding my project, adding in the files one at a time, and I think I've got it...
There was a subview of the view in question which had userInteractionEnabled == YES. Once I set this to NO, my view controller started getting touchesBegan/Moved/Ended calls for each touch. Presumably, the second touch was being claimed by the subview and not making it to my view controller, but I have no idea why this would only happen with the second touch and not the first.
I haven't figured out what's going on with the cocos2d project yet, but presumably it's a different issue as there are no subviews or nodes that could be doing the same thing.

Resources