IOS: disable gesture in a viewcontroller - ios

In my app I have this situation:
I have my main viewcontroller where I alloc a "flipwiecontroller" and add its view in this way
self.flipViewController.view.frame = CGRectMake (...);
[self.view addSubview:self.flipViewController.view];
and at this flipViewController I add some gesture (as pangesture, swipegesture and tapgesture for some control that I use on it)
when I press a button in this view I alloc another viewcontroller "paintingviewcontroller" in this way
[self.view addSubview:paintingViewController.view];
in this second view controller I have some buttons and another function, but when I try to do a swipegesture or a tapgesture it recognize the events of my "flipviewcontroller"
I don't understand, if I add a view controller over another viewcontroller, why gesture of flipviewcontroller are active yet?
thnaks

Maybe you are disabling userIntercation on the paintingViewController, then, it's events are sent to his superview.
Also you can use [UIView removeGestureRecognizer:UIGestureRecognizer] to remove gestures.

How did you add the gesture recognizers? They should be added to the view, not the window. Perhaps that's the issue.
In case you have something like this:
[self.view.window addGestureRecognizer:panGestureRecognizer];
You should change it to this:
[self.view addGestureRecognizer:panGestureRecognizer];

Related

How to implement common tap Gesture which will work for all View controllers

i want to use one common tap gesture for show/hide slider view in view controller with multiple View controller classes.
please provide solution how to implement this for ios .
slider view will show / hide by tap of view of View Controller.
and slider view contain tableview so tableview cell also be select when user click on tableview cell in ios.
is there any way to create a Abstract class for that.
thanks in advance.
You can add tapgesture to main content view of VC like,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:recognizer];
And hanlde tap in method like,
-(void)handleTap : (UITapGestureRecognizer*)recognizer {
NSLog(#"tap detect");
}
Hope this will help :)

how to get touches of uiimage view in scrollview and perform some action

I have a scrollview with uiimageview1 added on it. On click on that uiimageview1 I want to present a viewcontroller to make some drawing. After drawing, I want to take picture of that drawing and place it on uiimageview1.
i don't know how to and where do i present that viewcontroller for drawing.
please help.
So, first you need to detect touches on your imageView. To do that you need to use UIGestureRecognizer or UIButton.
Then, you need to present a new view controller using the presentViewController:animated:completion: method of your current UIViewController (passing it the 'drawing' view controller).
After the drawing is performed, you can use iOS delegation pattern to notify the original view controller that you just drew something.
The original view controller handles that 'delegated event' and updates the image view accordingly.
UIViewController and UIButton documentations are here for you.
Create a UIButton with uiimageview.layer.bounds and add it as subview to uiimageview
Make the button background color [UIColor clearColor]
Add a #selector to button and push newViewController with segue or add it programaticaly from UINavigation Controller stack.
Otherwise you can make a pan gesture recognizer to UIImageView and listen for user interaction TAP on screen for pushing the new view controller
You can Simply do this by adding UITapGestureRecognizer properties to your image view.
Like:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(presentViewController)];
[self.yourImageView addGestureRecognizer:tapGesture];
-(void)presentViewController{
// do remain stuff
}
Happy Codding :)

I've hidden the navigation bar and the status bar, now the screen edge pan gesture to go back won't work, is this typical?

I'm curious, if I set the navigationBar to hidden, and also hide the status bar, my view controller no longer responds to the screen edge gesture to pop the view controller.
Is this expected behaviour? I tried to set the interactivePopGestureRecognizer to enabled in viewDidLoad after I hide the navigation bar, but it still won't work.
[self.navigationController.navigationBar setHidden:YES];<--doesn't remove pop gesture
[self.navigationController setNavigationBarHidden:YES];<-- disables pop gesture
Simply use the first option, and in your root controller's viewDidAppear method use:
[self.navigationController.navigationBar setHidden:NO];
Are you sure you've done things right? I've put together an example that seems to work for me. All I did was make the navigationController.navigationBar.hidden = YES and [[UIApplication sharedApplication] setStatusBarHidden:YES]
-edit-
After closer inspection, it looks like there are two different properties on UINavigationController. There is navigationBar which is the UINavigationBar view, and there is navigationBarHidden which is a boolean. When you set navigationBarHidden to true, the swipe gesture stops working. But if you set that actual view to be hidden with navigationBar.hidden then the gesture still works. Check the Git repository for an example.
A very simple work around:
Link a swipe gesture method to your navigation back button. Make the current view controller the target of the gesture recognizer (self), with the selector popThisViewController. Then install the gesture recognizer into the view that the user will swipe on. Don't forget to add your go back action
edit added swipe gesture for reference to other coders that don't know
Cleaner code will look like this:
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(goBack:)];
gesture.numberOfTouchesRequired = 1;
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
}
-(IBAction)goBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}

UILongPressGestureRecognizer on UIButton not working

I have a longpress gesture recognizer that I create in ViewDidLoad then attach to a button like this, the button is created in the storyboard and linked to my class.
UILongPressGestureRecognizer *hold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(secretChange:)];
hold.minimumPressDuration = 5.0;
hold.delegate = self;
[_button addGestureRecognizer:hold];
The class conforms to the GestureRecognizer protocol and I have my selector here
- (void)secretChange:(UILongPressGestureRecognizer *)sender {
// Some stuff
NSLog(#"Secret");
}
The selector is not being called and I cannot figure out why, this seems to be the code everyone gives out on the internet, I have tried removing the minimum duration to make sure I didn't accidentally set it ridiculously long
UPDATE: I am actually adding this gesture recognizer to multiple buttons like this
[_button1 addGestureRecognizer:hold];
[_button2 addGestureRecognizer:hold];
[_button3 addGestureRecognizer:hold];
What is happening is the gesture recognizer is only being applied to the last button I add it to. How do I get the gesture recognizer added to ALL the buttons? Do I need to make a new one for every button?
You should have three instance of UILongPressGestureRecognizer.
Before add a gesture recognizer to a new view, the addGestureRecognizer method will remove the gesture recognizer from the view it has been attached to.

Swipe gesture between view controllers in storyboards

I am looking to add a left and right swipe gesture to change between view controllers, is this possible and is there an easy way to do this in storyboards? Thanks :)
Storyboards allow you to set up segues between two view controllers. I would say start by attaching segues between the views, give it an identifier.
Then use something like
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myRightAction)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self addGestureRecognizer:recognizer];
to add a swipe recognizer to a view.
Then in the myRightAction method you'd want to do your segue call like
[self performSegueWithIdentifier: #"MySegue" sender: self];
to go to a view or dismiss it to leave the view.
You can use Navigation View Controllers to have a right and left transition animation. You can also make custom segues.
on Xcode 5 it is simpler, less code needed:
storyboard -> utility panel -> object Library -> drag and drop the Swipe Gesture Recognizer inside your scene
you MUST drag it into the view itself(where the background color appears) so you can see this connection: referencing outlet collections -> view
add the delegate by dragging the delegate's connection pin to the viewController
Show assistant editor -> add an action by ctrl + drag and drop on your delegate class viewController.m
inside that action write
[self performSegueWithIdentifier:#"mySegueIdentifier" sender:self];
Oops. Posted a comment on accident. Take a look at this answer: https://stackoverflow.com/a/14125580/1126783. You could just use a swipe gesture instead of buttons, and it uses storyboards.

Resources