How can I except a part from a UIView for a gesture recognizer? - ios

I am developing an app and I want to scroll through different UIViewControllers with a UISwipeGestureRecognizer.
Everything works, but there is one thing where I couldn't find a solution for.
I add the UIGestureRecognizers like this
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
}
Now I want to know if it is possible to except a part of the self.view where it is possible to swipe to a different view.
I want to do this around a UISwitch.
Can someone please help me?
Best regards,
Lukas Goes

You can use another UIView and add it to self.view. Those UISwipeGestureRecognizer won't work inside that UIView.

Add another UISwipeGestureRecognizer to the switch, and, on your original swipe gesture recognisers, call requireGestureRecognizerToFail: on the switch's new recognizer.
Hope that makes sense, it's such a verbose class... :\

I suggest you to use the delegate method of a gesture recognizer : gestureRecognizer:shouldReceiveTouch:.
In the touch parameter, you have many information, like the position in the screen. You can use this position and test if it is above the switch frame. Return yes or no depending of if you want to trigger the selector associated to the gesture recognizer or not.

Related

disable swipe gesture for iCarousel

I am using iCarousel to display an array of images and I want to disable the swipe gesture. I did not find that in the documentation. not sure if this is doable or not
If you want to disable the swipe gesture then I think are you want to do something like programatically change the image.
For very simply disable the user interaction of carousel.
If you using storyboard then simple remove checkmark of User Inreaction Enabled
If you use by code then following code to disable the User Inreaction Enabled
yourcarousel.userInteractionEnabled = FALSE;
May this help lot to solve your problem.
#Junchao GU If you are Using
https://github.com/nicklockwood/iCarousel
They are using Tap gesture and pan gesture
You have to Comment
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
panGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[_contentView addGestureRecognizer:panGesture];
//add tap gesture recogniser
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTap:)];
tapGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[_contentView addGestureRecognizer:tapGesture];
in iCarousel.m File
I hope this will help you
It's bad idea to change source code of iCarousel. I think it's better to do next:
carouselView.contentView.gestureRecognizers?.removeAll()
Hope it helps to someone

Add vertical swipe to UIPageViewController

I have a horizontal swipe of my UIPageViewController that moves between 3 view controllers. I want to add a Vertical swipe onto this to add a extra level to the navigation.
However I have not found a solution to do so. I have tried adding a simple
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.view addGestureRecognizer:gestureRecognizer];
But this doesn't work as I presume the UIPageView overrides this.
Is there any solution?
If you are doing with storyboards,this tutorial would be helpful
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/
I am using pageview controller in my app. I have added same code as yours. and my swipe up event is working fine.
change this line in your code
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];

How to detect swipe direction?

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.

WebView and UISwipeGestureRecognizer

I am trying to have a UISwipeGestureRecognizer that is attached to a webview. This works, but I need to also be able to scroll up and down the web view.
Here is my code:
UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:upSwipeGesture];
[webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:upSwipeGesture];
If I comment out the last line, then the webview scrolls but the gesture is not recognized. If I do not comment out the last line, then the gesture is recognized but the webview does not scroll.
I would like to be able to scroll and have the gesture be recognized. Any ideas? Thanks!
UIWebView has its own private views, which also has gesture recognizers attached. Hence, precedence rules keep any gesture recognizers added to a UIWebView from working properly.
One option is to implement the UIGestureRecognizerDelegate protocol and implement the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer. Return YES from this method for other tap gestures.
This way you'll get your tap handler called, and the web view will still get its called.
Try this,
UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
upSwipeGesture.delegate=self;
[self.view addGestureRecognizer:upSwipeGesture];
ind implement this delegate method,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

IOS: Detect swipe gestures in a specific place on a UIView

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)

Resources