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;
}
}
Related
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");
}
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];
}
I have 2 views: view with tag 1 and view with tag 2.
Each view has two gesture recognizers.
CGRect tempRect=CGRectMake(0, 0, 100, 100);
for (int i=1; i<=2; i++) {
UIView *tempView=[[UIView alloc] initWithFrame:tempRect];
UIImageView *tempImageView=[[UIImageView alloc]initWithFrame:tempRect];
tempImageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#%d%#",#"image_",i,#".png"]];
UISwipeGestureRecognizer *tempRecognizer = [[UISwipeGestureRecognizer alloc] init];
[tempRecognizer performSelector:#selector(SwipeRight:) withObject:[NSNumber numberWithInt:i]];
[tempRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[tempView addGestureRecognizer:tempRecognizer];
[tempView addSubview:tempImageView];
tempView.tag=i;
[self.view addSubview:tempView];
}
I have a method: SwipeRight that manages the swipe....
-(void)SwipeRight:(NSNumber*)MyTag{
int MyProgr = [MyTag intValue];
}
Which is the correct way to pass the tag?
With my code I got an error:
[UISwipeGestureRecognizer SwipeRight:]: unrecognized selector sent to instance...
Set the View Tag before
[tempView setTag:i];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(callYourMethod:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[tempView addGestureRecognizer:swipeRight];
- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
int tagValue = recognizer.view.tag;
NSLog(#"Down");
}
}
Please try to use this one
-(void) SwipeRight:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
int tag = recognizer.view.tag;
}
}
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