UITapGestureRecognizer to detect single touch - ios

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

Related

Adding both short long gestures in same view with proper differentiation

I am working on app in which I want both short long gesture in same view, I added but issue is I'm facing is that short gesture end always call, Need your help how to do that in right way. Below is my code.
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self addGestureRecognizer:longGesture];
UILongPressGestureRecognizer *shortGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(shortGesture:)];
shortGesture.minimumPressDuration = 0.1f;
[shortGesture setDelegate:self];
[self addGestureRecognizer:shortGesture];
- (void)longGestureOnFormFields:(UIGestureRecognizer*) recogniser
{
if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
- (void)shortGesture:(UIGestureRecognizer*) recogniser
{
if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
You can manage that by using one UITapGestureRecognizer like,
self.view.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self.view addGestureRecognizer:longGesture];
UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(testm)];
[self.view addGestureRecognizer:recog];
and your methods smething like,
-(void)testm{
NSLog(#"tap");
}
-(void)longGestureOnFormFields : (UILongPressGestureRecognizer*)sender{
if (sender.state == UIGestureRecognizerStateEnded ) {
NSLog(#"long press gesture");
}
}
I have added gesture recognizer in viewDidload i.e in viewcontroller so i have add it to self.view if you have subclass UIVIew and adding this to that view then add it on self like [self addgesture...] as shown in question!

Swipe left & right works all the time, but swipe up/down doesn't

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.

Using AMSlideMenu, re-enabling swipe gesture

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.

UISwipeGestureRecognizer blocked by UITapGestureRecognizer

I have a view which needs to handle pan, tap, and swipe gestures. I have pan and tap working, but when I add swipe, it doesn't work. Curiously, it seems that tap somehow blocks the swipes because if I remove the tap, then swipe works fine. Here's how I create my gesture recognizers.
- (void) initGestureHandlers
{
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftGesture:)];
swipeLeftGesture.numberOfTouchesRequired = 1;
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeLeftGesture];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRightGesture:)];
swipeRightGesture.numberOfTouchesRequired = 1;
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeRightGesture];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[tapGesture setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGesture];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
}
A lot of blog suggest using requireGestureRecognizerToFail to handle the case where tap fires before the double tap fires, so I tried that, but it also didn't work.
[tapGesture requireGestureRecognizerToFail:swipeLeftGesture];
[tapGesture requireGestureRecognizerToFail:swipeRightGesture];
How can I get tap and swipe in the same view?
comment or remove the lines
//[tapGesture requireGestureRecognizerToFail:swipeLeftGesture];
//[tapGesture requireGestureRecognizerToFail:swipeRightGesture];
Also set the all the guesture objects delegate to self. like this
- (void) initGestureHandlers
{
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftGesture:)];
swipeLeftGesture.numberOfTouchesRequired = 1;
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeftGesture.delegate = self;
[self addGestureRecognizer:swipeLeftGesture];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRightGesture:)];
swipeRightGesture.numberOfTouchesRequired = 1;
swipeRightGesture = self;
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeRightGesture];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[tapGesture setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGesture];
tapGesture.delegate = self;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
panGesture.delegate = self;
}
Implement the delegate method like this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
put this one and try it,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
{
return YES;
}

detect from which recognizer a gesture is coming

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;
}
}

Resources