Been reading up on Gesture swiping and how to push on or off UIViews depending on the direction. I just to have come to an issue that I haven't been able to work out. I can recognise the touch events i.e. up, down, left, right. However when I try to segue to another screen (within the storyboard) I receive an error that is so vague I'm not sure where to begin. I have seen other apps do this so clearly it can be done, just have not found the correct tut or example. Below is the code if anyone had an idea.
- (void)viewDidLoad
{
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(didSwipe:)];
swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(didSwipe:)];
swipeDown.numberOfTouchesRequired = 1;
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDown];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(didSwipe:)];
swipeLeft.numberOfTouchesRequired = 1;
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(didSwipe:)];
swipeRight.numberOfTouchesRequired = 1;
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
[super viewDidLoad];
// Do any additional setup after loading the view.
// [self.view removeGestureRecognizer:swipeUp];
// [self.view removeGestureRecognizer:swipeDown];
// [self.view removeGestureRecognizer:swipeLeft];
// [self.view removeGestureRecognizer:swipeRight];
}
FirstViewController *fvc;
-(void)didSwipe: (UISwipeGestureRecognizer *) sender
{
UISwipeGestureRecognizerDirection direction = sender.direction;
switch (direction)
{
case UISwipeGestureRecognizerDirectionRight:
NSLog(#"you swiped the screen right");
[self performSelector:#selector(goRight:)];
break;
case UISwipeGestureRecognizerDirectionLeft:
NSLog(#"you swiped the screen left");
[self performSelector:#selector(goLeft:)];
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(#"you swiped down");
//[self performSelector:#selector(goDown:)];
break;
case UISwipeGestureRecognizerDirectionUp:
NSLog(#"you swiped Up");
//[self performSelector:#selector(goUp:)];
break;
default:
break;
}
}
-(void)goRight:(id)sender
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"ip5" bundle:[NSBundle mainBundle]];
UIViewController* controller = [storyboard instantiateViewControllerWithIdentifier:#"FirstView"];
controller.transitioningDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
Related
i had an array which is contains 10 items which is 1,2,3,4,5,6,7,8,9,10. when i load the page the item is at 3. so when the user swipe left it will go to 4 and swipe to right it will go to 2. may i know how to do it?
newsID=[[NSMutableArray alloc]init];
[newsID addObject:#"1"];
[newsID addObject:#"2"];
[newsID addObject:#"3"];
[newsID addObject:#"4"];
[newsID addObject:#"5"];
[newsID addObject:#"6"];
[newsID addObject:#"7"];
[newsID addObject:#"8"];
[newsID addObject:#"9"];
[newsID addObject:#"10"];
UISwipeGestureRecognizer * swipeleft=
[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer * swiperight=
[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
Try this.
i am taking one UILabel and changing the text of it while swiping.and one int variable to keep track of swipe.
- (void)viewDidLoad
{
[super viewDidLoad];
newIdArray = #[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10"];
num_index = 3;
ValueLabel.text = [newIdArray objectAtIndex:num_index];
leftGesture = [[UISwipeGestureRecognizer alloc]init];
leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[leftGesture addTarget:self action:#selector(PerformSwipegesture:)];
[self.view addGestureRecognizer:leftGesture];
rightGesture = [[UISwipeGestureRecognizer alloc]init];
rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[rightGesture addTarget:self action:#selector(PerformSwipegesture:)];
[self.view addGestureRecognizer:rightGesture];
}
- (IBAction)PerformSwipegesture:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
if (num_index <9)
{
num_index = num_index +1;
ValueLabel.text = [newIdArray objectAtIndex:num_index];
}
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
if (num_index >0)
{
if (num_index >1)
num_index = num_index -2;
else
num_index = num_index -1;
ValueLabel.text = [newIdArray objectAtIndex:num_index];
}
}
}
Here is simple example for handling swipe gestures. Here i am taking one UILabel and changing the text of it while swiping.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_lblText.text=#"3";
_lblText.textAlignment=NSTextAlignmentCenter;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipe)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
}
-(void)handleRightSwipe
{
if ([_lblText.text intValue]>1)
{
_lblText.text=[NSString stringWithFormat:#"%d",[_lblText.text intValue]-1];
}
else
{
return;
}
}
-(void)handleLeftSwipe
{
if ([_lblText.text intValue]<10)
{
_lblText.text=[NSString stringWithFormat:#"%d",[_lblText.text intValue]+1];
}
else
{
return;
}
}
I have an object based on UIView that I've set up to recognise swipe gestures. The view has another UIView based object in the middle of it.
When I swipe left or right, regardless of where I put my finger, the correct event fires.
When I swipe up or down the correct event only fires if my finger isn't starting in the subview in the middle?
Why?
Here's the init code for the object:
- (id)init
{
self = [super init];
if (self) {
[self setFrame:CGRectMake(0,
0,
[self getScreenWidth],
[self getScreenHeight])];
backView = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
[self getScreenWidth],
[self getScreenHeight])];
backView.backgroundColor = [UIColor whiteColor];
[self addSubview:backView];
[self bringSubviewToFront:backView];
theCard = [[actCard alloc] init:CARD_POS_CENTER];
[backView addSubview:theCard];
[backView bringSubviewToFront:theCard];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipeLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[self addGestureRecognizer:swipeUp];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[self addGestureRecognizer:swipeDown];
NSLog(#"Enabled: %d",swipeLeft.isEnabled);
NSLog(#"Enabled: %d",swipeRight.isEnabled);
NSLog(#"Enabled: %d",swipeUp.isEnabled);
NSLog(#"Enabled: %d",swipeDown.isEnabled);
}
return self;
}
Try setting the delegate of your Upward swipe and downward swipe to be your MainViewController.Something like :
swipeDown .delegate = self;
swipeUp .delegate = self;
This should make your main view controller ( which contains the secondary UIView ) responsible for the gestures received.
Hope this helps!
Obvious! What a newb I am.
The UITextView still had scrollEnabled. That means the UITextView took the up & down swipes, they don't get passed on to the view.
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 have 2 imageview, and I want them to rotate when they are clicked. This is my code:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognizerMethod:)];
Then in the tapRecognizerMethod:
- (void)tapRecognizerMethod:(UITapGestureRecognizer *)tapRecognize
{
if (tapRecognize == tapRecognizer) // tapRecognizer is the first imageviews recognizer
{
if (tapRecognize.state == what should go here?
{
//do something
}
}
}
What should go after tapRecognize ==?
- (void)tapRecognizerMethod:(UITapGestureRecognizer *)tapRecognize{
if (tapRecognize == tapRecognizer){
if (tapRecognize.state == UIGestureRecognizerStateEnded){
UIImageView *imgView = (UIImageView*)[tapRecognize view];
//Now need to rotate the image
}
}
}
Have a look at the following link:
rotating Image
- (void)handleTap:(UITapGestureRecognizer *)tapRecognize
{
if (tapRecognize == tapRecognizer)
{
CGAffineTransform transform = CGAffineTransformRotate(lineImage.transform, 90);
[lineImage setTransform:transform];
}
}
and make sure you have enabled the uesr interaction of imageviews,
[imageView1 setUserInteractionEnabled:YES];
[imageView2 setUserInteractionEnabled:YES];
If you actually requires a flip rotation you better chose below swipe gestures.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleSwipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipeLeft];
[swipeLeft release];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeRight];
[swipeRight release];
On handler method you give an animation for separate flip right and left
if(isActiveFlipLeft)
{
[UIView transitionWithView:imageSnap
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
imageSnap.backgroundColor = [UIColor colorWithRed:187.0/225.0 green:187.0/225.0 blue:187.0/225.0 alpha:1.0];
imageSnap.image = image;
} completion:NULL];}
else if (isActiveFlipRight)
{
[UIView transitionWithView:imageSnap
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
imageSnap.backgroundColor = [UIColor colorWithRed:187.0/225.0 green:187.0/225.0 blue:187.0/225.0 alpha:1.0];
imageSnap.image = image;
} completion:NULL];}
The below code is for just a simple tap gesture. you will get call on handleSingleTap:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
[singleTap release];
Gesture handler
- (void)handleSwipeLeft:(UIGestureRecognizer *)gestureRecognizer;
- (void)handleSwipeRight:(UIGestureRecognizer *)gestureRecognizer;
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer;
If you requires real rotation you can use
imageview.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI );
something like in your gesture handler
I have simple xcode project just made from "Master-Detail Application" template, for iPad. When device is in Portrait orientation, master view is hidden and when you swipe right on detail view, master view will show up. Now, i want to add right swipe gesture recognizer to detail view, like that:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(void)swipeHandler{
NSLog(#"SWIPE");
}
But this code causes that when i swipe on detail view, "SWIPE" log appears in console, but master view doesn't show up.
How to add right swipe gesture recognizer to detail view, so it wont prevent master view to show up and my handler for recognizer will work?
Thanks in advance.
EDIT. I want my right swipe recognizer handler to work simultaneously with this built in one, which shows up master view, but that following code isn't a solution for this specific situation:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
you should set the direction for the swipe in order to add the right swipe
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];
and your swipe handler might look like
-(void)swipeHandler:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRecognizer:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight;
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];
}
- (void)swipeRecognizer:(UISwipeGestureRecognizer *)sender {
if (sender.direction == UISwipeGestureRecognizerDirectionRight){
[UIView animateWithDuration:0.3 animations:^{
CGPoint Position = CGPointMake(self.view.frame.origin.x + 100.0, self.view.frame.origin.y);
self.view.frame = CGRectMake(Position.x , Position.y , self.view.frame.size.width, self.view.frame.size.height);
[self.navigationController popViewControllerAnimated:YES];
}];
}
}
Try This
//Right Swipe
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandlerRight:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];
//Left Swipe
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandlerLeft:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:gestureRecognizer];
call method
-(void)swipeHandlerRight:(id)sender
{
//Your ViewController
}
-(void)swipeHandlerLeft:(id)sender
{
//Your ViewController
}
This works. It pushes the view controller from the navigationController.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler:)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(IBAction)swipeHandler:(UISwipeGestureRecognizer *)sender
{
NSLog(#"SWIPE");
UIViewController *tb = [[DetailViewController alloc] init];
tb = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
[self.navigationController pushViewController: tb animated:YES];
}
Then be sure to go to storyboard (or you could also do this manually) - click on the Detail View Controller and give the View controller the Identity: DetailViewController
swift 4.0
Step 1: Add swipe Gesture(s) in viewDidLoad() method.
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
Step 2: Check the gesture detection in handleGesture() method
#objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizerDirection.right {
print("Swipe Right")
}
else if gesture.direction == UISwipeGestureRecognizerDirection.left {
print("Swipe Left")
}
}