Pan speed calculation to perform different tasks in swift - ios

I have a set of objects in a UIScrollView that I am able to do the pan gesture. However, I would like to be able to scroll the view if i'm doing a swipe even if i hold the object and swipe too fast. Is it possible for me to calculate the speed of the drag and drop so i can make the UIScrollView move instead of executing a drag and drop of the swipe is fast?
What would be the solution for this? Please advise

There may be a simpler way. You can ask a gesture recognizer to wait for another to fail before starting. It seems to me that it is what you want to achieve. Can you try :
[scrollView.panGestureRecognizer requireGestureRecognizerToFail:myGestureRecognizer];
or the other way
[myGestureRecognizer requireGestureRecognizerToFail:scrollView.panGestureRecognizer];

Related

Using Pan and Swipe gesture recognisers simultaneously

I've got a view on the bottom of my ViewController ( similar to Google Maps Bottom Sheet ). The goal is:
When panning up, the view moves towards the pan direction ( essentially follows the finger), when panning ends, the view goes fullscreen. So far so good, all works.
Adding swipe gestures. When swiping up, the view should go full screen.
The issue is that by definition, a swiping gesture is a pan gesture but not the other way around. So unless i go really slow with my panning , the swipe gesture will trigger and the view will go full screen even though im still dragging on the screen.
Simply panning up doesnt look much like the kind of swipe im looking for. The swipe gesture im describing should only trigger if the user "flicked" the view momentarily. If they keep on panning the pan gesture should take precedence.
Any ideas how to achieve this? For reference you could check tap on a pin on google maps on either android or ios.
Its a little hard to describe without showing so if it helps im very open to clarify things.
UPDATES
I think the distinction for a swipe that would override the pan as im describing is that it a) took a short amount of time to complete b)the gesture ended with the user lifting the finger off the screen c) (maybe wrong ) the area traversed should not be too big. Sounds a lot like a flick to me..
If you cannot describe the difference between the two gestures in clearly defined terms then it will be difficult to tell the UIGestureRecognizer how to do so. In addition, it is possible that your user will have difficulty figuring out how to properly interact with the screen if your gestures are too similar or complex.
That being said, you may be able to distinguish between a "swipe" and a "pan" by checking the gesture's velocity. You should be able to play around with the UIGestureRecognizerDelegate methods and achieve the effect you want.
Ok i've achieved what was needed. The short answer is that i was looking for a "flick" gesture instead of a pan gesture. Generally its a nono to add custom gestures but as im replicating the google maps bottom sheet component for ios, it would appear that a custom gesture is the only way.
Using Navillus's comment, I added a velocity and gesture end check in the pan recogniser. The result looked like this:
if(recognizer.state == .ended){
if(recognizer.velocity(in: self).y > CGFloat(500) ){
self.pullUpViewSetMode_SUMMARY()
return;
}
if(recognizer.velocity(in: self).y < CGFloat(-500) ){
self.pullUpViewSetMode_FULL()
return;
}
}
//the rest of the pan handling code, namely translating the view up and up follows here
Hope this helps somebody.

How to Handle All gestures

In my iPad project i have to make
1.Tap gesture
2.Swipe right and Swipe left Gesture
3.Single Finger Pan Gesture
4.Two finger Pan gesture.
5.Long press gesture
I don't know how to handle all these gestures.When Long press gesture is used only Swipe right gesture and two finger Pan gesture has to work. App gets confused at these many gestures in a single screen. How to Handle everything easily. Please help
Firstly i will say your question was little confusing and we can easily add multiple UITapGestures on a singleView without any prob. The OS automatically detects which gestures you are doing. There are many tutorials for that. Look at these links, if you still can find any solution, tell me may be i can try a code piece for u.
Link 1
Link 2
and lastly
Link 3

Accurate start position for UIPanGestureRecognizer?

I am using a UIPanGestureRecogniser to implement drag and drop. When the drag starts I need to identify the object that is being dragged. However the objects are relatively small. And if the user doesn't hit the object right in the centre of the object it isn't getting dragged.
The problem is that when the gesture handler is first called with the state UIGestureRecognizerStateBegan, the finger has already moved several pixels, and so [UIPanGestureRecognizer locationInView:] returns that point, which is not where the gesture truly started. That makes sense as it can only recognize a pan after a few pixels of movement. However, I need the absolute start of the gesture, not the position after the gesture has first been recognized.
I'm thinking that maybe I need to implement a tap gesture recognizer as well, purely to capture the first touch. But that seems like a hack for what is not an unusual requirement. Is there no other way of getting that first touch from within the pan gesture recognizer?
UIGestureRecognizerDelegate protocol provides methods gestureRecognizerShouldBegin: and gestureRecognizer:shouldReceiveTouch: that can help you evaluate the touches before the pan has transitioned to state UIPanGestureRecognizerStateBegan

Slide Effect for iOS

I'm new to developing iOS apps,
I've successfully implemented a Swipe Gesture Recognizer,
What I was wondering is if there is an easy to use recognizer like the swipe gesture. That would let you implement the homescreen page turning effect but just on a small view in the view controller?
If your unclear on what effect I mean, when you look at the iPhone's homescreen you can drag your finger and it responds instantly (unlike swipe) and also has some spring feeling to it, is this some effect I can use, or do I manually have to program this into the code if so is there a tutorial that explains this?
Thanks,
I hope my question makes sense.
Have a look at UIPanGestureRecognizer:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html
UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer
that looks for panning (dragging) gestures. The user must be pressing
one or more fingers on a view while they pan it. Clients implementing
the action method for this gesture recognizer can ask it for the
current translation and velocity of the gesture.
A panning gesture is continuous. It begins
(UIGestureRecognizerStateBegan) when the minimum number of fingers
allowed (minimumNumberOfTouches) has moved enough to be considered a
pan. It changes (UIGestureRecognizerStateChanged) when a finger moves
while at least the minimum number of fingers are pressed down. It ends
(UIGestureRecognizerStateEnded) when all fingers are lifted.
Clients of this class can, in their action methods, query the
UIPanGestureRecognizer object for the current translation of the
gesture (translationInView:) and the velocity of the translation
(velocityInView:). They can specify the view whose coordinate system
should be used for the translation and velocity values. Clients may
also reset the translation to a desired value.
Edit: The spring feeling part you would need to implement yourself. Since iOS 7 there is UIDynamics which contains different animators, for what you describe you may need UIGravityBehavior and maybe UICollisionBehaviour. Look at the WWDC 2013 videos for this topic, I think you will find some examples there.

iOS drag and drop images

I was trying show a bunch of images, one at once. User could swipe left or right to navigate, just like photo library.
What I did is, for example I have 6 images, create a scroll view to hold those 6 images, and using pan gesture to move it.
Am I doing right? Or is there another neat solution?
Thanks
EDIT:
I'm asking am I doing right by using gesture?
More specific, should I use UIScrollView to do this or I should use something else?
Update:
I finally figure out a way to do. Use UIPanGestureRecognizer and check the speed. if the speed is fast enough, then treat like swipe gesture. Will post code later.
UIScrollView has a default gesture and does not need a new one.

Resources