How to switch viewcontrollers programmatically by swipe guesture? - ios

I want to put the push Navigation into swipe.
i want to call this method on swipe gesture.
- (IBAction)MakeADifferenceButtonPressed:(id)sender {
MakeADifferenceViewController *makeADifferenceView = [[MakeADifferenceViewController alloc] initWithNibName:#"MakeADifferenceViewController" bundle:nil];
[self.navigationController pushViewController:makeADifferenceView animated:YES];
[makeADifferenceView release];
}
any-one please help me. i am new in iphone.

Use this code to add UISwipeGestureRecognizerDirectionLeft in your current UIView
UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(MakeADifferenceButtonPressed:)];
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

Related

How to add custom gesture to a tab in UITabBarController

I have a tabbarController:
UITabBarController* tabBarController = [[UITabBarController alloc] init];
UIViewController* view1 = [[UIViewController alloc] init];
UIViewController* view2 = [[UIViewController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, nil];
I want to add a long press gesture to view1's tab button(called tabBarItem)
or just add a long press gesture to the tabbar.
How could I do?
Thanks. Any advice would be appreciated.
Just put your UITabbarController Methods
UITabBarController *tabBarController = [[UITabBarController alloc] init];
and than use this for add gesture->
UILongPressGestureRecognizer *longRecog = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(longPressed)];
[tabBarController.tabBar addGestureRecognizer:longRecog];
and the selector is -
-(void)longPressed{
//Long pressed Occures
}

using swipe gesture recognizer swipe from one viewcontroller to another

Swipe-Right
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideToRightWithGestureRecognizer:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
Swipe-Left
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideToLeftWithGestureRecognizer:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
On 'slideToRightWithGestureRecognizer' method i want to write code which opens abcViewController which has a viewcontroller on storyboard and same with 'slideToLeftWithGestureRecognizer' to open xyzViewController which also has view controller on storyboard. Without using Navigation View Controller in the program
Should be very easy to accomplish:
When the ViewControllers are connected on your storyboard you could easily do it like this:
- (void)slideToLeftWithGestureRecognizer:(id)sender {
[self performSegueWithIdentifier:#"myConnectionIdentifierHere"]
}
If not:
- (void)slideToLeftWithGestureRecognizer:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"myViewControllerIdentifierHere"];
[self presentViewController:vc animated:YES completion:nil];
}
Have you considered using UIPageViewController? Or does it necessarily have to be a swipe and not a pan gesture (UIPageViewController provides navigation between view controllers using a pan gesture interactively).

Cannot push to a XIB iOS

In my app I tried to push to a XIB using a long tap event. I did the following code,
In viewDidLoad
UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)]autorelease];
longPress.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.view addGestureRecognizer:longPress];
[longPress requireGestureRecognizerToFail:doubleTap];
and longPress method is
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
ProductDetailViewController *vc = [[[ProductDetailViewController alloc] initWithNibName:#"ProductDetailViewController" bundle:nil] autorelease];
[self.navigationController pushViewController:vc animated:YES];
NSLog(#"Long Press");
}
}
I have a XIB named ProductDetails.xib and I set the File's Owner of it as ProductDetailViewController.
When I'm running this code NSLog is working. But it's not going to the ProductDetails.h. And also There are no errors. How can I fix this? Anyone can help me with this.
Thanks in Advance!
You already have the xib hooked up to the controller, you can achieve that with the following.
ProductDetailViewController *vc = [[[ProductDetailViewController alloc] init] autorelease];
[self.navigationController pushViewController:vc animated:YES];

UIPopoverController and UITapGestureRecognizer not working in ViewDidLoad

When I add a UIPopoverController in the ViewDidLoad method, it displays an empty popover view. The same thing happens when I add a UITapGestureRecognizer; it doesn't trigger the attached method. If I put the code in my ViewDidAppear it does work, but in some cases ViewDidAppear is never called, depending on where in my storyboard the view is loaded.
Here's my code:
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view = popoverView;
pickerPopup = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
pickerPopup.delegate = (id)self;
[pickerPopup setPopoverContentSize:CGSizeMake(320, 240) animated:NO];
And the UITapGestureRecognizer:
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPicker:)];
[_form_from addGestureRecognizer:tap];
I solved it for now using a delay in the ViewDidLoad method like so
[self performSelector:#selector(custom_init) withObject:nil afterDelay:0.1];
Still don't get why the UITapGestureRecognizer doesn't trigger. It does work if I attach it to the view, but not if I attach it to a textfield in a static cell.

Unable to get touch on my UIImageView - iOS

I need to make my every single UIImageView to be single tapped.
I have a viewController, on this view controller there is a view which i push another views on this view with containment.
I am trying to create gallery ,
What might I be doing wrong?
This is my code;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Custom initialization
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[self addGestureRecognizer:singleTap];
[self setBackgroundColor:[UIColor lightGrayColor]];
}
return self;
}
-(void)openPhotoDetail
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SideMenu" bundle:[NSBundle mainBundle]];
PhotoDetailViewController *photoDetailViewController = [storyboard instantiateViewControllerWithIdentifier:#"photoDetailViewController"];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.window.rootViewController.menuContainerViewController.centerViewController pushViewController:photoDetailViewController animated:YES];
}
Initially a UIImageView has user interaction disabled. Enalbe the user interaction of your UIImageView and it should work.
imageView.userInteractionEnabled = YES;
In Viewdidload set
YourImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(openPhotoDetail:)];
// Instead of self ,give your imageView to add gesture event
[YourImageView addGestureRecognizer: singleTap];
Click on the UIImageView, on storyboard,
Go to Attributes Inspector (The button that looks like a tuxedo, top right ,Second button).
Check the attribute "User Interaction Enabled"
Or if you want to do it by code,
in
-(void)viewDidLoad {
myImageView1.userInteractionEnabled = YES;
myImageView2.userInteractionEnabled = YES;
myImageView3.userInteractionEnabled = YES;
}
UIImageView *MyImageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYZ.png"]];
UITapGestureRecognizer *TapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[MyImageview addGestureRecognizer:TapGesture];
[MyImageview setUserInteractionEnabled:YES];
Can u use it like that

Resources