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.
Related
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 :)
I have an image and in the storyboard I've dragged a Tap Gesture Recognizer on top of this image and changed the settings:
For my TGR:
For my image:
How do I hook this up to my controller now? I want a method to fire off when the double tap happens. Is there some sort of protocol I have to conform to? Do people normally do this from the storyboard or programmatically in viewDidLoad? I don't mind doing it another way if that's the general trend of things.
connect the gesture recognizer as a outlet to your ViewController and in your viewDidLoad :
[self.yourGesture addTarget:self action:#selector(didTapOnImage:)] ;
and then declare your method didTapOnImage method :
-(void)didTapOnImage:(UIGestureRecognizer*) recognizer
{
//do your work here
}
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'm using a UISplitViewController and one of the detail view controllers contains a view which has a UIPanGestureRecognizer added to it. When I swipe this view in the detail view controller, the gesture is recognized, but the swipe gesture recognizer of the split view controller interferes with it; the master view controller is displayed and the gesture recognizer from the detail controller is ignored.
Implementing and debugging the shouldRecognizeSimultaneouslyWithGestureRecognizer method from the UIGestureRecognizerDelegate shows two UIPanGestureRecognizer objects: one from the detail view controller and the one from the split view controller, so I'm certain they are interfering with each other.
When I set presentsWithGesture = NO on the split view controller, the gesture recognizer inside the detail view controller does work. But that disables the gesture recognizer on the split view controller, so it's not really a solution to the problem.
I've also tried disabling the gesture recognizer on the split view controller, only when I need the other gesture recognizer to work, but it seems that it is not possible to set presentsWithGesture once the split view controller has become visible.
I've also tried to disable the default gesture on the split view controller and adding a custom gesture which I can control, but it didn't work. I tried using the target and action from the split view controller barbutton on the gesture, but it doesn't work. Calling toggleMasterVisible: on the split view controller isn't an option either because it's part of the private api.
Does anyone have any suggestions on how to handle this?
I would suggest that you disable the UISplitViewController pan gesture when you need the other one to work. This should do it:
for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[recognizer setEnabled:NO];
}
}
You probably don't want to search for it each time, so I would store a reference to that gesture recognizer when the view loads, then just disable and enable as appropriate:
on viewDidLoad:
for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[self setSplitViewPanGesture:recognizer];
}
}
later:
[self.splitViewPanGesture setEnabled:NO];
and later:
[self.splitViewPanGesture setEnabled:YES];
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];