in my viewDidLoad I set
UISwipeGestureRecognizer *swipeRecognizerU = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeUpDetected:)]; swipeRecognizerU.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipeRecognizerU];
when I load a new view through popup I need to disable that gesture
// show popup view
-(IBAction)showPopup:(id)sender
{
MJDetailViewController *detailViewController = [[MJDetailViewController alloc] initWithNibName:#"MJDetailViewController" bundle:nil];
[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];
}
after popup view is dismissed, I need to set swipe gesture back.
// hide popup view
-(IBAction)hidePopup:(id)sender
{
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideBottomBottom];
}
how this can be done?
I think there is property named enabled for UIGestureRecognizer. Have you try this, it should be ok to disable your swipes:
swipeGestureRecognizer.enabled = NO;
You need set delegate at here.
Ex :
swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
swipeleft.delegate = self;
[self.view addGestureRecognizer:swipeleft];
Then add function
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ((touch.view == test[1]) || (touch.view == test[2]) || (touch.view == test[3])) {
[gestureRecognizer setCancelsTouchesInView:YES];
[swipeleft setCancelsTouchesInView:YES];
[gestureRecognizer setEnabled:NO];
[swipeleft setEnabled:NO];
return NO;
}
else
{
[gestureRecognizer setCancelsTouchesInView:NO];
[swipeleft setCancelsTouchesInView:NO];
[gestureRecognizer setEnabled:YES];
[swipeleft setEnabled:YES];
return YES;
}
}
I think useful for you
Related
I am working on an application which works like a remote control of different devices like Game Console, Set-top Box, etc.
Application contains different type of controls like gesture and buttons to perform events as remote.
Single gesture view contains multiple gesture recognizer as follow:
1 finger Tap using UITapGestureRecognizer
2 finger Tap using UITapGestureRecognizer
1 finger swipe using UISwipeGestureRecognizer
2 finger swipe using UISwipeGestureRecognizer
My requirement is to implement 1 finger Swipe gesture recognizer along with hold event. So, that will continue my swipe action until user will not remove its finger from the view.
I have tried UILongPressGestureRecognizer after UISwipeGestureRecognizer but this didn’t work for me because it is executing along with Swipe movement. And I want this after Swipe end, but finger will not remove.
My code snippet :
-(void) gestureView:(UIView*)viewGesture {
UISwipeGestureRecognizer *swipeGestureSingleRight;
UISwipeGestureRecognizer *swipeGestureSingleLeft;
UISwipeGestureRecognizer *swipeGestureSingleUp;
UISwipeGestureRecognizer *swipeGestureSingleDown;
swipeGestureSingleUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleUp setDelegate:self];
[swipeGestureSingleUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
[swipeGestureSingleUp setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleUp];
swipeGestureSingleDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleDown setDelegate:self];
[swipeGestureSingleDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
[swipeGestureSingleDown setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleDown];
swipeGestureSingleRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleRight setDelegate:self];
[swipeGestureSingleRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[swipeGestureSingleRight setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleRight];
swipeGestureSingleLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleLeft setDelegate:self];
[swipeGestureSingleLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[swipeGestureSingleLeft setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleLeft];
longPressGestureOnSwipe = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressGesture_Handle:)];
longPressGestureOnSwipe.minimumPressDuration = 0.03;
longPressGestureOnSwipe.delegate = self;
[viewGesture addGestureRecognizer:longPressGestureOnSwipe];
isSwipeEnd = false;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
- (void)swipeGestureSingle_Handle:(UISwipeGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
singleTouchSwipeSenderDirection = sender.direction;
isSwipeEnd = true;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
}
-(void)longPressGesture_Handle:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
isSwipeEnd = false;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (isSwipeEnd) {
[super touchesBegan:touches withEvent:event];
} else {
return;
}
}
Please suggest me correction or solution for my requirement.
Thanks in advance!
I need to dismiss the presentViewController when click the outside the controller, I'm using the following code
UITapGestureRecognizer *tapBehindGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapBehindDetected:)];
[tapBehindGesture setNumberOfTapsRequired:1];
[tapBehindGesture setCancelsTouchesInView:NO];
[self.view.window addGestureRecognizer:tapBehindGesture];
- (void)tapBehindDetected:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window
//Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.
if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
{
// Remove the recognizer first so it's view.window is valid.
[self.view.window removeGestureRecognizer:sender];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
}
its working fine for me, but i had problem in another view. Inside the view controller I add UITapGestureRecognizer to vwHeaderview.
UITapGestureRecognizer *addNewContactsingleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[vwHeaderview addGestureRecognizer:addNewContactsingleFingerTap];
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
//Some code
}
If I click the vwHeaderview It call the "tapBehindDetected", I wont call "handleSingleTap".
If I comment above "tapBehindGesture" Its working fine. But I need both to work. any help
I add delegate to vwHeaderview and use following function & its working fine for me...
UITapGestureRecognizer *addNewContactsingleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
addNewContactsingleFingerTap.delegate = self;
[vwHeaderview addGestureRecognizer:addNewContactsingleFingerTap];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Need help using the AMSlideMenu from https://github.com/SocialObjects-Software/AMSlideMenu
In a project I'm creating I needed to disable the AMSlideMenu swiping so I could use the UISwipeGestureRecognize to perform another action.
The main swiping has been disabled [self disableSlidePanGestureForLeftMenu]; and the menu is accessible by pressing the menuBtn, but I would like to use UISwipeGestureRecognizer which AMSlideMenu seems to be disabling.
Any ideas on how I could regain the UISwipeGestureRecognizer for my project?
Thank you!
- (void)viewDidLoad
{
[super viewDidLoad];
[self disableSlidePanGestureForLeftMenu];
UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[menuBtn addTarget:self
action:#selector(menuButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:menuBtn];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:rightSwipe];
}
- (void)menuButtonTapped:(id)sender
{
[self.mainSlideMenu openLeftMenu];
}
-(void) swipeRecognized:(UISwipeGestureRecognizer *)swipe {
int pageNum;
switch (swipe.direction)
{
case UISwipeGestureRecognizerDirectionLeft:{
pageNum++;
break;
}
case UISwipeGestureRecognizerDirectionRight:{
pageNum--;
break;
}
default:
break;
}
}
Change gesture in Table view
- (void)viewDidLoad
{
[super viewDidLoad];
[self disableSlidePanGestureForLeftMenu];
UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[menuBtn addTarget:self
action:#selector(menuButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:menuBtn];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.tableView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(swipeRecognized:) ];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableView addGestureRecognizer:rightSwipe];
}
- (void)menuButtonTapped:(id)sender
{
[self.mainSlideMenu openLeftMenu];
}
-(void) swipeRecognized:(UISwipeGestureRecognizer *)swipe {
int pageNum;
switch (swipe.direction)
{
case UISwipeGestureRecognizerDirectionLeft:{
pageNum++;
break;
}
case UISwipeGestureRecognizerDirectionRight:{
pageNum--;
break;
}
default:
break;
}
}
And now Just only Add an delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
You need to stop left/right pan gesture from left menu controller class.
And then put your swipe gesture for specific view controller.
Follow steps below to stop swipe gesture for left menu:
1) Go to AMSlideMenuMainViewController.m class, in that select - (void)setup method.
2) Comment following lines from this method will stop swipe gesture.
self.panGesture = [[UIPanGestureRecognizer alloc] init];
[self.panGesture addTarget:self action:#selector(handlePanGesture:)];
[self.currentActiveNVC.view addGestureRecognizer:self.panGesture];
3) After commenting these lines create a swipe gesture in your ViewController and handle your custom logic.
I've set up my UIView to call a method when the view is panned up and to call some other method when the view is swiped up. My pan works fine and I can keep my pan disabled if my velocity.y is above a certain limit, but I can never get the swipe action to work when my pan fails. I've tried playing around with the delegate methods without much luck. Looked over this solution but no luck: https://stackoverflow.com/a/21728621/1925859
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panRec.delegate = self;
[panedView addGestureRecognizer:panRec];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeRec.delegate = self;
[panedView addGestureRecognizer:swipeRec];
[swipeRec requireGestureRecognizerToFail:panRec];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
if (ABS(velocity.y) > 200)
{
NSLog(#"Swipe actiavted");
return NO;
}
}
return YES;
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}
If you remove your delegate method, add direction to the swipe and change the receiver of requireGestureRecognizerToFail then it will work. Note that pans must be way slower than sweeps in order to be recognized.
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[panedView addGestureRecognizer:panRec];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
[panedView addGestureRecognizer:swipeRec];
[panRec requireGestureRecognizerToFail:swipeRec];
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}
- (IBAction)handlePan:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"PAN");
}
I am trying to mix an UISwipeGestureRecognizer with an UITableView.
What I would like to do is, while I am doing the swipe gesture, move at the same time the UITableView outside the window and refresh the table data.
I am going to show you with images...
This is my view:
My View
And I would like to get something like this:
Desired View
I am able to move the table when the swipe gesture is ended, but not while I am doing the gesture, and that is what I want.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
swipeToRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToGroup:)];
[swipeToRight setDelegate:self];
[swipeToRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self table]addGestureRecognizer:swipeToRight];
swipeToLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToContact:)];
[swipeToLeft setDelegate:self];
[swipeToLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self table]addGestureRecognizer:swipeToLeft];
}
- (void)changeToGroup:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
- (void)changeToContact:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
I thought that I could do it with UIGestureRecognizerStateBegan, adding the animations inside that event, but I can't receive it...
Could anyone help me?
Thanks a lot!!.
Try this,
use UIPanGestureRecognizer
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan:)];
[recognizer setMaximumNumberOfTouches:1];
[recognizer setDelegate:self];
[[self table]addGestureRecognizer:recognizer];
and in
- (void) handlePan:(UIPanGestureRecognizer *)recognizer {
UIView *view = self.tableView;
if(recognizer.state == UIGestureRecognizerStateBegan) {
// remember where the pan started
//panGestureOrigin is a CGPoint variable
panGestureOrigin = view.frame.origin;
//optional to keep an enum to keep direction
//self.panDirection = MenuPanDirectionNone;
}
CGPoint translatedPoint = [recognizer translationInView:view];
if(translatedPoint.x > 20) {
//self.panDirection = MenuPanDirectionRight;
[self handleRightPan:recognizer];
} else if(translatedPoint.x < 0) {
//self.panDirection = MenuPanDirectionLeft;
[self handleLeftPan:recognizer];
}
}
- (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
//animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
}
- (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
// animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
}