Passing a touch gesture - iphone

I'm looking for a solution to help "pass" a touch gesture along.
Basically I have a menu, and I want users to be able to drag and drop items from the menu to the canvas in a single continuous drag.
I have already achieved a draggable image via pan gestures (we'll call these instances Sprites). I can also instantiate a Sprite anywhere on the UIView using a button or UIImageView with touch gestures.
However, this currently requires two touches. One to touch down the menu item button and release, creating the Sprite. The second to touch down on the sprite, allowing the user to drag it, and then release it where they want. I would like to merge these touches so that when a user touches a menu item, the Sprite is instantiated and already within the pan gesture, or something to that affect.
I've attached a visual description if that helps. Any help is appreciated. Thanks!

There is no way of artificially forcing UIGestureRecognizer to recognize touches that are passed to a different view.
From the Gesture Recognizer docs:
Delivery of events initially follows the usual path: from operating
system to the application object to the window object representing the
window in which the touches are occurring. But before sending an event
to the hit-tested view, the window object sends it to the gesture
recognizer attached to that view or to any of that view’s subviews.
Figure 3-1 illustrates this general path, with the numbers indicating
the order in which touches are received.
Figure 3-1
Event's delivery happens automaticaly by the system and is delivered to the appropriate view.
To do what you want I would implement the UIGestureRecognizer on the subview (view that contains your UIButton) and on press create an instance of your Sprite object and manipulate that object from withing the gesture recognizer of the subview. Alternatively you could use -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)even to reposition the object yourself.

Related

iOS Touches Began

What I want: Touch a button and a view is added right where the touch is. Without having to lift the finger the touches began/moved automatically begins working on the UIView. So without lifting the finger, I have touched the button and can drag the new view around.
What I don't know how to do:
Stop the touch events on the button and immediately send the touch events to the new view that is directly under the finger.
One option could be to ditch the button and just use the uiview touches to detect when to add the subview you want to let the user drag...
Daniel
As #Daniel suggested ditch the button and just use a UIView, but I believe you may need to have a UIPanGestureRecognizer in place to get your dragging.
You could set a flag when a new UIView is created and then forward any gesture events to that view - only while the user still has their finger down from the initial touch.
After the user has lifted their finger the new view can just deal with gestures by itself by adding a UIPanGestureRecognizer to it.

UIView over another uiview and pass touches through

I have a view sitting over another view. The top view has a UITapGestureRecognizer so I can close the menus (click outside of the menus). But the layer below this needs to receive all the touches.
I can get single finger gestures to pass through, but I can't get the pinch gesture to pass through.
You need to make sure that multiple touches are enabled in both views. That way, a pinch can be recognized! Hope that helps!

Cocos2d: One layer on top of another - Is it Possible to temporarily deactivate touches for a certain layer/region?

I have a scene where there is a button. Once I click the button, a rectangular sprite slides in from the left side: http://img255.imageshack.us/img255/9867/slidei.png
Under this shape, there are several touch sensitive buttons. I don't want these to be called when I touch on the rectangular shape. So, as long as the shape remains on the screen, those touches should not respond. Instead, there are several other buttons on top of that brown shape, that respond to touches. How can I manage that?
Is it possible to temporarily deactivate touches for a certain layer in a scene? Has the rectangular shape to be a CCLayer Object on its own?
I know I could create a new scene for that shape that has a transparent background, BUT I still want the button to react to touches:
When I click that button, the shape slides in. When I click it again, it slides off the screen.
Depends on if you are using actual button objects or CGRect regions that your touchesBegan method responds to. I agree with GamingHorror that the cleanest approach would be to enable/disable the button objects directly as needed.
However, this is a workable kludge as long as the sliding touch region is a UIView subclass.
Before it slides in you can disable all user touches with:
[[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:NO];
After your view slides in, you may need to setUserInteractionEnabled:YES on that particular view. When it slides out, you can put it all back with
[[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:YES];
Like with any user interface, you have to let the objects know whether they are enabled or not. Your best bet is actually to send a message to the button, telling it to turn itself on or off.
Ideally you'll be using a global touch input handler, instead of allowing each individual button or slide to react inputs by themselves. This spells a lot of trouble and extra work. Instead, put all objects that should react to input on the same layer, and register that layer with the touch input handler, which will then forward all touch events to that particular layer and no other.

How can I "catch" a subview during a swipe gesture on an iOS device?

I'm building an iOS board game (similar to scrabble or words) that involves moving around little tiles on the screen and I'm finding the user sometimes has a hard time touching and moving the tiles around because they're too small. Due to the design of the game, I can't increase the size of the tiles, so I've had to implement some little tricks that make touching and moving the tiles easier for the user and they work well. The only issue that remains is the user sometimes touches just barely outside the tile and when the user tries to move it, the tile stays where it is.
I have two ideas for how I can fix this...
If a tile isn't touched when the user touches the screen, I can use the parent view's touch location to find the closest tile to the touch location and somehow forward the touch event to that tile's view.
If a tile isn't touched when the user touches the screen, I can somehow "catch" the tile when the user drags their finger over it as they attempt to move it.
I'd prefer to implement solution #2 since solution #1 has too many problems associated with, not to mention solution #2 is a more realistic experience. That said, how can I "catch" a tile when the user drags their finger over it and send it touch events to move it where the finger is?
Currently, my tiles are implemented as subclasses of the UIView and they handle the touch events (touchesBegain, touchesMoved, and touchesEnded) directly. So if the user touches just barely outside the view and drags their finger over the view, it doesn't receive any of the touch events since it didn't receive the initial touchesBegan event.
Thanks in advance for all your wisdom!
Maybe you should have the "board" view handle all the dragging. When a touch begins and there is a tile at that point, then start dragging it. Otherwise check whenever the touch is moved and as soon as you find a tile, start dragging it.
You could override hitTest:withEvent: in the board view so that it can still detect when a touch hits a tile, but always return itself so that touch events go to the board view (e.g. record the subview that was hit in a separate member variable, so that you know what to start dragging later on when touch events start coming in).
More Details
When handling touches, UIView will use hitTest to find the view that should receive touch events. The default implementation checks each subview so that the "deepest" subview in the hierarchy gets the touches. In order for the board view to receive touches, you would have to disable userInteraction on all of the tile views. But that means you can't use hitTest to find the tile that was touched, since it ignores views that have userInteraction disabled.
So what I am saying is leave userInteraction enabled on the touch views. But override hitTest on the board view so that it first calls the super implementation in order to find a tile (if the result is self, the board itself was hit). No need to implement your own tile searching. Something like this:
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if ( hitView != self )
self.draggingTile = hitView;
return self;
}
Now you know what tile to move in touchesMoved. However, I don't think hitTest is called as the touch is moved, so if no tile has been picked up yet, you may have to call it manually (you can get the point and event from the touch passed to touchesMoved.
Have you looked into the UIGestureRecognizer API? I think your best option would be to add a UIPanGestureRecognizer recognizer to your board's view which would then fire back the selector to your UIViewController.
Setup in ViewDidLoad:
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan:)];
[[self view] addGestureRecognizer:panGestureRecognizer];
And then implemented the selector:
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer{
CGPoint currentPoint = [gesture locationInView:[self view]];
}
When you set up the gesture recognizer you can set parameters to limit the callbacks (only recognize pans with 1 finger, etc. And in the callback you can check the gesture properties to see if the pan in continuing or if it's coming to an end. You can also grab the current point and determine if it's in or near a tile.

How to flick through a deck of cards?

I'm making an iPhone cards game where each use has a deck of cards, I'd like to give the user the option to drag his finger through the cards and each card get's highlighted while his finger is on it. This effect is already done in Uno for iPhone. My cards are put into UIButton, what i tried to do is to set a small image to the button in normal state and a bigger image in the highlighted state, it did achieve the effect i was looking for but, the user has to highlight each card individually to be able to see the bigger picture.
Here is the code i used to set the normal and highlighted state of the UIButton:
//player413 is an IBOutlet to a UIButton, and img,imgHigh are UIImages
[player413 setImage:img forState:UIControlStateNormal] ;
[player413 setImage:imgHigh forState:UIControlStateHighlighted] ;
Any guidelines ?
Use a single view to handle all the touch interaction.
EDIT: Oh, all right.
When a view receives a touch hitTest:withEvent: is called recursively, until a view which "accepts" the touch is found.
Once hitTest:withEvent: returns a non-nil value, it's over (by default); that view "owns" the touch (see UITouch.view). Only that view gets touchesBegan/Moved/Ended/Cancelled:withEvent: callbacks.
If you want the touch to affect any card in the deck, the deck should implement touches*:withEvent:, and either set userInteractionEnabled=NO on subviews, or override hitTest:withEvent: so it returns "self" instead of one of the "cards".
Then, in touches*:withEvent:, detect which "card" the touch is on, and then do card.highlighted = YES. If you have multipleTouchEnabled=NO, you can assume that there's only one touch and use UITouch * touch = [touches anyObject].
(There are a handful of UIKit classes which somehow sit between the touch and its owning view: UIScrollView can intercept the touch and scroll instead; gesture recognizers cancel touches when they detect a gesure.)