I want to create custom Gesture Recognizer.
For Example:
if user pan the screen from top to bottom some distance from there if the finger move left direction some distance, i need to invoke some method from view. same if the finger move right direction means some other function should get invoke.
For this functionality
1. can i use UIPanGestureRecognizer and detect the user interaction throw the translation position,
2.There is any way to implement my own custom GestureRecognizer
(like: UIPanLeftLGestureRecognizer for user drag the finger from top to bottom some distance and moved left from there, UIPanRightLGestureRecognizer for user drag the finger from top to bottom some distance and moved right from there.)
Which is the best way to achieve the solution for this problem. kindly guide me.
Thanks in advance.
I would use two UISwipeGestureRecognizers with different directions like this:
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
And don't forget to add these gesture recognizers to appropriate view
Related
My collection view is working great. It shows a grid of photos and lists hundreds of them. You can swipe vertically to scroll through them all. Life is good. However, I now have a new requirement. I need to be able to detect when the user is swiping left or right. I need to be able to intercept this gesture so I can attach behavior to left and right swipes while keeping intact my collection view's vertical scroll capabilities. Any ideas?
You need two recognizers, one for swiping left, and the other for swiping right:
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
and for the handler:
- (void)handleSwipeLeftFrom:(UIGestureRecognizer*)recognizer {
}
Finally, you add it to your view:
[view addGestureRecognizer:swipeUpGestureRecognizer];
The same for the other direction (just change all the Lefts to Rights).
add UISwipeGestureRecognizer to the cells settings its direction as below,
UISwipeGestureRecognizer *swipeRightDir = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeRightDirection:)];
swipeRightDir.delegate = self;
swipeRightDir.numberOfTouchesRequired = 1;
[swipeRightDir setDirection:UISwipeGestureRecognizerDirectionRight];
I would like to detect when a user makes a swipe gesture, and detect it's direction (left, right, up or down). I need to detect it not when the gesture is finished, but just when the iPhone knows the direction, even if it's not finished.
I am using XCode 5, and designing for iPhone 5 with iOS 7.
I would like to know the code that I have to paste to the .h, and to the .m, and if I have to drag and drop any item to the mainView.
If you want to know the direction when the gesture is not finished you would probably need the UIPanGestureRecognizer and detect the direction using the velocityInView: method.
in the *.h file:
#interface ViewController : UIViewController
#property (nonatomic, strong) UIPanGestureRecognizer *recognizer;
#end
in the *.m file:
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGesture:)];
[self.view addGestureRecognizer:self.recognizer];
}
-(void)panGesture:(UIPanGestureRecognizer *)sender
{
NSLog(#"Velocity: %#", NSStringFromCGPoint([sender velocityInView:self.view]));
// Here You need to determine the direction
}
#end
From Docs:
Gestures are either discrete or continuous. A discrete gesture, such
as a tap, occurs once. A continuous gesture, such as pinching, takes
place over a period of time. For discrete gestures, a gesture
recognizer sends its target a single action message. A gesture
recognizer for continuous gestures keeps sending action messages to
its target until the multitouch sequence ends,
So if you need to "detect it not when the gesture is finished, but just when the iphone knows the direction, even if it's not finished" you could not use UISwipeGestureRecognizer since it is a discrete one. You should use UIPanGestureRecognizer and analyse it is results to check is there a swipe gesture or not.
To have a good example how it works, you can download a sample code provided by Apple:
https://developer.apple.com/library/ios/samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460
Hope that helps ;)
You can easily get using UISwipeGestureRecognizers property directions...
After :----
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeRight)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.yourView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeft)];
swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
[self.gifWebView addGestureRecognizer:swipeLeft];
-(void)swipeRight{
//Do whatever you want
}
-(void)swipeLeft{
//Do whatever you want
}
And For Before Swipe you can use UIPanGestureRecognizer
I hope it help you
Swipe gestures are very quick. Swipe gesture recognizers are designed to fire once, not continuously. I don't think you will be able to get information about a swipe gesture that is "in flight" very easily.
If this is urgent, you will probably have to create a custom subclass of swipe gesture recognizer and add additional logic and delegate messages that would notify the delegate once the gesture recognizer decides that it has detected a swipe. That is way beyond your current skill level however.
I'm trying to add a very simple swipe up gesture to one of my views. The gesture does not work at all unless I apply an outward/opposite pinch gesture with two of my fingers. I am creating the gesture connections via IB. I have set up the gesture properties to Swipe: Down and Touches: 1. However it will only work with a 2 finger outward pinching motion? What is going on? Thanks.
Edit:
Ok, I've tried coding it out rather than using interface builder and I'm getting the same results. Here is the code I'm using
UISwipeGestureRecognizer *swipeUpOnVideoView = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(SwipeUp:)];
swipeUpOnVideoView.direction = UISwipeGestureRecognizerDirectionUp;
swipeUpOnVideoView.numberOfTouchesRequired = 1;
[self.playerView addGestureRecognizer:swipeUpOnVideoView];
As you can see I am specifically designating only 1 touch, however my SwipeUp: method will only execute if I use an outward pinching motion with two fingers???
check this it working proper in my application:
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeUp:)];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp ];
[self.view addGestureRecognizer:swipeUp];
- (IBAction)swipeUp:(UISwipeGestureRecognizer *)recognizer
{
// do you stuff here
}
May be it will help you.
Happy coding...:)
It turned out I had a number of other UITapGestureRecognizers that were on my Xib file. Even though I wasn't calling them in the code anywhere they were associated with views on my page via the connections inspector. For some reason this interference made it so that my single finger swipeGesture would only register if I used a two finger pinching motion.
Deleting multiple UITapGestureRecognizers from my xib and then writing the code for the UISwipeGestureRecognizer programmatically fixed the problem for me.
I am creating a storyboard app where the view is changed when the user performs swipe gestures. The issue I am having is that when you drag and drop a gesture recognizer onto the view from themain.storyboard file, the gesture is recognized from anywhere inside the UIView. Basically, I need to recognize a gesture that is performed in a specific area on the screen, similar to how you drag down the notification center in IOS 6. If this is unclear or you need more details, feel free to ask.
Thanks in advance for any help!
I am not sure if this will help you or you tried something like this, but i want to share my idea.
You can try UISwipeGestureRecognizer in your ViewControl.m like below:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeToDoMethod)];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];
[[self innerView] addGestureRecognizer: swipeGesture];
You can add an inner view into your main view and add this gesture to that view.
Hope this helps you, good luck! :)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeView)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self innerView] addGestureRecognizer: swipe];
//UISwipeGestureRecognizerDirectionRight (or) up (or) down
Use another UIView with 0 visibility, put it on area where you want recognize a gesture and then add gesture to this UIView.
This is just an idea.
Hope this will help you.
You can add a subview into your view, set the subview's frame to the area where you want to catch the swipe gesture, add recognizer to it, and set its background color to clear color. Hope it help.
Adding Swift version:
//Add gesture recognizer
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(dismissShowMessageAndButtonDueToSwipeUp))
swipeGesture.direction = .up
backgroundView.addGestureRecognizer(swipeGesture)
Which gesture am I looking to capture if I want to detect when the user slides his/her finger upwards (think of scrubbing through a song, that kind of motion, but up) and adjust a setting as a result (I don't want to use a UISlider).
What would you guys recommend for this? Is that a pan gesture recognizer or a swipe?
Try this:
UISwipeGestureRecognizer *swipeRecognizer =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(swipeDetected)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
A swipe is one-time simple motion; observe that it happened, and you respond afterwards. A pan gesture is a continuous movement that you want to able to track while it happens. Which of those do you want?