Detecting UIView grid squares where a UIPanGestureRecognizer begins and ends - ios

I'm designing a game that takes place on a grid. The grid needs to respond to pan gestures that begin in one square and end in another. I have a custom view controller class called GameVC which contains the grid of UIViews of subclass GameGridSquare. I want the game to perform an action when, for example, a pan gesture begins and ends in neighboring squares. I have a storyboard wired with properties that name each square by row and column: self.A1, self.A2, ... self.H7, self.H8. As a pan gesture is recognized, I want the GameVC to receive the two GameGridSquares where UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded so it can determine the appropriate action, like so:
-(void)validatePanGestureFrom:(GameGridSquare*)beginSquare
to:(GameGridSquare*)endSquare
What's the best way to do this? My recognizers are functional and returning coordinates, but I think I need to explore hit-testing.
If I add recognizers to the individual GameGridSquares, I get undesired results. For example, a pan that begins in A1 and ends in B2 would be recognized by A1 alone. This suggests that I need a pan recognizer on GameVC that can detect when the gesture begins and ends in separate subviews.
From what I've read, I believe that the UIGestureRecognizerDelegate protocol may be helpful here. I also understand that a custom UIPanGestureRecognizer subclass would allow me to override hitTest:withEvent but I'm not sure where to even begin with that. Any ideas about how I should approach this?

This was what I was after:
Find which child view was tapped when using UITapGestureRecognizer
UIView* view = gestureRecognizer.view;
CGPoint loc = [gestureRecognizer locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];

Related

Is there a way to make a UITextField move when user drags across screen?

I'm new to coding so I'm trying some small projects in swift. Right now, I'm trying to make a text box inside the ViewController move when the user drags it along the screen. For the text box, I am currently using a UITextField but I have no idea how to program its movement according to drag.
You'll want to add a UIPanGestureRecognizer to your view. There's all sorts of built in gesture recognizers for detecting various gestures like a tap or in this case a pan (drag). You can check them out here: https://developer.apple.com/documentation/uikit/uigesturerecognizer
Here we'll create a pan gesture recognizer, and add it to our view. Assume myView is your UITextField. A good place to do this is in your view controller's viewDidLoad() method.
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(sender:)))
myView.addGestureRecognizer(pan)
The moment your finger touches the screen, we say that a touch sequence has begun. The touch sequence ends when there are no more fingers on the screen. The pan gesture will determine if this touch sequence looks like a pan, and if so, the method handlePan will be called at various stages. Here, the gesture itself will be passed into the method, which we use to determine translation and move our view accordingly. Add this as a method of your view controller.
#objc func handlePan(sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: sender.view)
self.myView.center.x += translation.x
self.myView.center.y += translation.y
sender.setTranslation(CGPoint.zero, in: sender.view)
}
The first line gets the translation in the view which the gesture is attached to (myView). We then adjust myView's position based on this translation, and then we set the translation to zero. This is so that the next time this method is called, the translation will be a delta relative to the previous call.
The property sender.state will tell you the state the gesture is currently in, for example, .began, .changed, .ended. Since a pan is a continuous gesture, our method will be called many times, whenever there's a finger movement.

Holding two UIPanGestureRecognizer

I want to make my UIView swipeable by X-axis and by Y-axis separately. For example if user swipes view vertically it triggers one action and if user swipes view horizontally it triggers another action. I don't know how to implement this correctly so I'm thinking of attaching two UIPanGestureRecognizers to my view. Is it wrong?
Just use a single UIPanGestureRecognizer and use its translation(in: UIView?) and velocity(in: UIView?) functions to determine which direction the user is swiping.
Don't use two Gesture use only single UIPanGestureRecognizer and call this method with your panGesture:
-(void)moveVerticallyAndHorizentally:(UIPanGestureRecognizer *)gesture{
CGPoint velocity = [gesture velocityInView:self.view]; // you can use your own view
if (fabs(velocity.y) > fabs(velocity.x)) {
// vertical motion
}
else if (fabs(velocity.x) > fabs(velocity.y)){
// Horizental motion
}
}
Hope this will help you.

Set exclusive touch on multiple UIViews of the same class

I am creating a random number of custom UIViews of the same class, and I'm adding them in the UIViewController's view. I'm assigning them a UITapGestureRecognizer, but I can't seem to make the exclusive touch work:
for (int i = 0; i <= n; i++) {
ICCatalogProductView *catalogProductView;
catalogProductView = [[ICCatalogProductView alloc] init];
[self.view addSubview:catalogProductView]
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(testTouch)];
[catalogProductView addGestureRecognizer:tapGesture];
[catalogProductView setExclusiveTouch:YES];
}
If i tap the UIViews simultanously, the method is called twice (not the behaviour I want). Is there any elegant method of solving this, or any method at all?
From the Apple Documentation:
exclusiveTouch only prevents touches in other views during the time in
which there's an active touch in the exclusive touch view. That is, if
you put a finger down in an exclusive touch view touches won't start
in other views until you lift the first finger. It does not prevent
touches from starting in other views if there are currently no touches
in the exclusiveTouch view.
To truly make this view the only thing on screen that can receive
touches you'd need to either add another view over top of everything
else to catch the rest of the touches, or subclass a view somewhere in
your hierarchy (or your UIWindow itself) and override
hitTest:withEvent: to always return your text view when it's visible,
or to return nil for touches not in your text view.
means its only set exclusive in your one view, not if you are touching something outside your view.

How can I set up gesture recognizer to interact any UIView when all the views are being animated?

I found the code listed below from https://github.com/DuncanMC/iOS-CAAnimation-group-demo. This particular method allows the user to stop a UIView while it is "in-flight" in a core animation sequence using a gesture recognizer. When the view is tapped, the animation stops. As shown this code will only work on animated view. I have many animated views and I need interaction with any of the views. I think I must set up an array of views (or layers) and cycle through them. Is this correct? How could I do this? Thanks!
/*
This method gets called from a tap gesture recognizer installed on the view myContainerView.
We get the coordinates of the tap from the gesture recognizer and use it to hit-test
myContainerView.layer.presentationLayer to see if the user tapped on the moving image view's
(presentation) layer. The presentation layer's properties are updated as the animation runs, so hit-testing
the presentation layer lets you do tap and/or collision tests on the "in flight" animation.
*/
- (IBAction)testViewTapped:(id)sender
{
CALayer *tappedLayer;
id layerDelegate;
UITapGestureRecognizer *theTapper = (UITapGestureRecognizer *)sender;
CGPoint touchPoint = [theTapper locationInView: myContainerView];
if (animationInFlight)
{
tappedLayer = [myContainerView.layer.presentationLayer hitTest: touchPoint];
layerDelegate = [tappedLayer delegate];
if (((layerDelegate == imageOne && !doingMaskAnimation)) ||
(layerDelegate == waretoLogoLarge && doingMaskAnimation))
{
if (myContainerView.layer.speed == 0)
[self resumeLayer: myContainerView.layer];
else
{
[self pauseLayer: myContainerView.layer];
//Also kill all the pending label changes that we set up using performSelector:withObject:afterDelay
[NSObject cancelPreviousPerformRequestsWithTarget: animationStepLabel];
}
}
}
}
LOL. That demo project is mine. The code is written to find the layer that was tapped, and then use the fact that for a layer that backs a UIView, the layer's delegate is the view itself.
At the point in the code where it finds the layerDelegate, you should make sure it isKindOfClass UIView, then use whatever method is appropriate to match your view with the views you've animated. You could use an IBOutletCollection to keep track of the views that you are animating, or manually create a mutable array and add view objects to it, use view tags, or whatever makes sense for your application.

Shouldn't UIRecognizers fire when a subview is touched?

Views in my iPad app behave as if they prevent their superview's gesture recognizers from firing when the user initiates such gesture in that view.
Is this expected?
How can I remove that shielding behavior?
What are good practices to debug gesture recognizers?
In more details:
The main "canvas" view of my application, lets the user adds shapes to it with a "long double tap". I attached a gesture recognizer for such gestures to the main view. That works very well: the main view gets called, and reacts by adding a shape to the main view.
Shapes are implemented as subviews of the main view. When the user long-double-taps in the main view, my code instanciate a shape subview, and adds it to the main view. Shape views can be moved around with a long-single-tap recognizer. So I also attach a gesture recognizer for long-single-taps to every shape view. That works very well: the shape view gets called and lets the user move it in the canvas.
However, when the user long-double-taps in a shape view, nothing happens: the shape view is not called, which is expected since it doesn't have a gesture recognizer for long-double-taps. But the main view is not called either. I had thought that since the gesture was not recognized by the shape view, then it would be propagated up in the responder chain to the main view. But this doesn't happen.
My intent is to let the user add overlapping shapes to the main view, so that a long-double-tap on a shape would also add a new shape to the main view.
What could I have missed?
I can of course add a long-double-tap recognizer to shape views, and from there, either forward the gesture to the main view or handle the gesture directly in a way similar to what I do in the main view.
But this sounds wasteful, and more importantly, I'd like to understand the behavior.
Thanks for any suggestion.
It should as far as I can see pass the message along out of the box.
To ensure both gestureRecognizers are not fired you need to do something like:
[longPress requireGestureRecognizerToFail:doubleLongPress];
Update
Just free styling here but if you want to limit the gesture to one view you could try playing with the gesture delegate (this will only respond if the touched view is self.view)
self.myGesture.delegate = self;
In your controller do something like:
//.h
#interface MyController : UIViewController <UIGestureRecognizerDelegate>
// ...
#end
//.m
#implementation MyController
//...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
BOOL shouldReceiveTouch = YES;
if (gestureRecognizer == self.myGesture) {
shouldReceiveTouch = (touch.view == self.view);
}
return shouldReceiveTouch;
}
//...
#end
NB I haven't tested this but I will update when I test it later.

Resources