UISwipeGestureRecognizer in UIImageView - ios

I want to put two swipe gesture recognizers in a UIImageView but gestures aren't recognized. Only recognizes the tap gesture.
This is my code:
- (void)viewDidLoad {
//Acciones
img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[urls
objectAtIndex:index]]]];
UIImage *img2;
if (img.size.height > img.size.width) {
img2 = [[UIImage alloc] initWithCGImage: img.CGImage scale:1.0 orientation: UIImageOrientationUp];
}else{
img2 = [[UIImage alloc] initWithCGImage: img.CGImage scale:1.0 orientation: UIImageOrientationRight];
}
imageSelected.image = img2;
[imageSelected setUserInteractionEnabled:YES];
imageSelected.contentMode = UIViewContentModeScaleAspectFit;
imageSelected.backgroundColor= [UIColor whiteColor];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(exitAction:)];
[singleTap setNumberOfTapsRequired:1];
[imageSelected addGestureRecognizer:singleTap];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imageSelected addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imageSelected addGestureRecognizer:swipeRight];
}
Swipe function:
-(IBAction)leftSwipe:(id)sender{ NSLog(#"Left Swipe"); }
Thank you for advance.

The solution for me was:
Set a delegate for your gestures and return YES from
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

Related

How to change a UIView background colour upon swipe?

how can I change the UIView background colour by a SwipeGesture? e.g. Green for a Left Swipe and Red for a Right Swipe. Thank you very much.
code doesn't work:
- (void)viewDidLoad {
self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:0.5];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
[imageView setUserInteractionEnabled:YES];
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[self.view addGestureRecognizer:swipeLeft];
[self.view addGestureRecognizer:swipeRight];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
self.view.backgroundColor=[[UIColor alloc]initWithRed:138.0/255 green:24.0/255 blue:47.0/255 alpha:1.0];
NSLog(#"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:1.0];
NSLog(#"Right Swipe");
}
}
You need to have separate swipe handler to make it work. This works great.
Your codes in setting up your UISwipeGestureRecognizer should look
like this.
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
imageView.userInteractionEnabled = YES;
// Adding the swipe gesture on image view
[imageView addGestureRecognizer:swipeRight];
[imageView addGestureRecognizer:swipeLeft];
And your swipe handle should be like this.
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe {
self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:1.0];
}
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)swipe {
self.view.backgroundColor=[[UIColor alloc]initWithRed:138.0/255 green:24.0/255 blue:47.0/255 alpha:1.0];
}
Declare an imageView property in the interface of the ViewController file.
#property (weak, nonatomic) UIImageView *imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
[imageView setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[self.imageView addGestureRecognizer:swipeLeft];
[self.imageView addGestureRecognizer:swipeRight];
}
Handling Swipe Gesture Events
//newImage and newImage1 are the images files name in your project or ImageAssets
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"Left Swipe");
self.view.backgroundColor = [UIColor greenColor];
self.imageView.image = [UIImage imageNamed: #"myNewImage.png"];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(#"Right Swipe");
self.view.backgroundColor = [UIColor redColor];
self.imageView.image = [UIImage imageNamed: #"myNewImage1.png"];
}
}
Hope this helps. Happy Coding. refer to this SO Post

UIGestureRecognizer in objective-C++

I want to know the UIGestureRecognizer working or not in Objective-C++ because i've implemented this one but tap method never calling. So please let me know is it possible or not in Objective-C++.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imgView.image = [UIImage imageNamed:#"dharm"];
[self.view addSubview:imgView];
imgView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
tapRecognizer.numberOfTapsRequired = 1;
[tapRecognizer setDelegate:self];
[imgView addGestureRecognizer:tapRecognizer];
}
- (void)tap:(id)sender {
NSLog(#"Tap Pressed");
}
Try adding [imgView setUserInteractionEnabled:YES];

How to handle event of code-created UIView

I have unknown amount of UIImageViews which are created in the code, not in the xib file and i need to handle the taps on those images. The handling of each of this image view is going to be the same. How do i do this?
Demo code for your implemantation
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView1 setUserInteractionEnabled:YES];
[imgView1 addGestureRecognizer:singleTap];
imgView1.tag = 1;
imgView1.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView1];
UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView2 setUserInteractionEnabled:YES];
[imgView2 addGestureRecognizer:singleTap2];
imgView2.tag = 2;
imgView2.backgroundColor = [UIColor blueColor];
[self.view addSubview:imgView2];
}
-(void)tapDetected:(UITapGestureRecognizer *)gestureRecognizer{
UIImageView *myImg = (UIImageView*)gestureRecognizer.view;
NSLog(#"tag : %ld",(long)myImg.tag);
}
Try this code to add a gesture recogniser
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(action:)];
[_imgview addGestureRecognizer:tap];
_imgview.userInteractionEnabled = YES;
You can add UITapGestureRecognizer to detect touch on UIImageView.
Just use below method with argument, your image view and a unique tag, and you're done !
- (void) setTapGestureOnImageView:(UIImageView *)imageView withTag:(NSInteger)tag {
//this is important, by default user interaction isn't enabled, we have to enable it.
imageView.userInteractionEnabled = YES;
//create a tap gesture (in example this is sinlge tap) with target and action to call when user tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(detectTap:)];
//you can customize taps too
//tap.numberOfTapsRequired = 2;
//tap.numberOfTouchesRequired = 2;
//add gesture on image view
[imageView addGestureRecognizer:tap];
}
- (void)detectTap:(UIGestureRecognizer *)recognizer {
//Get the tapped image view from recognizer
UIImageView *imageView = (UIImageView *)recognizer.view;
//Check for condition, which image view tapped
if(imageView.tag == 1) {
//do something 1st imageview
}
else if(imageView.tag == 2) {
//do something for 2nd imageview
}
else {
//do something else
}
}
May be this will be useful to you
- (void)viewDidLoad
{
NSArray *imagesArray = [NSArray arrayWithObjects:#"statement_card_1.png", #"statement_card_2.png", #"statement_card_3.png", #"statement_card_4.png", #"statement_card_5.png", nil];
short xPadding = 10;
for (int i = 0; i< imagesArray.count; i++)
{
UIImage *imageRecipe =[UIImage imageNamed:[imagesArray objectAtIndex:i]];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPadding, xPadding+(i*60),imageRecipe.size.width, imageRecipe.size.height)];
imgView.tag =IMAGETAG + i;
[imgView setImage:imageRecipe];
imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
[imgView addGestureRecognizer:singleTap];
[self.view addSubview:imgView];
}
}
-(void)tapDetected:(UITapGestureRecognizer *)recognizer
{
UIImageView *TempImg = (UIImageView *)recognizer.view;
NSLog(#"tag : %ld",(long)TempImg.tag);
}

Swipe Gesture to Specific limit

I need to implement the swipe gesture to specific limit.
I tried using the below code
UIView *viewLeftSwipe = [[UIView alloc] initWithFrame:CGRectMake(30, 200, 100, 50)];
viewLeftSwipe.backgroundColor = [UIColor whiteColor];
viewLeftSwipe.userInteractionEnabled = YES;
[self.view addSubview:viewLeftSwipe];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[viewLeftSwipe addGestureRecognizer:swipeLeft];
But it is not calling the below method
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(#"Swipe");
}

UIGestureRecognizers interfering with UIToolbar?

I'm working on an OpenSource Magazine Engine that you can look at on GitHub:
https://github.com/interactivenyc/Defrag
I've set up a UIToolbar in a UIView I've called MenuPanel. For some reason the UIBarButtonItems in the UIToolbar aren't calling their actions properly. Here is the syntax I'm using for the buttons:
UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonClicked:)];
What is happening is that anywhere I click on my screen, the UITapGestureRecognizer declared in my main UIViewController is being called instead. This gets setup in this block of code in my main UIViewController:
- (void)setupGestureRecognizers {
//NSLog(#"setupGestureRecognizer NEW");
UISwipeGestureRecognizer *swipeRecognizer;
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
}
I'm sure I have something pretty basic conceptually wrong with how I'm trying to do this. Can someone tell me how I can fix this problem?
For reference, you can see my main DefragViewController: UIViewController here:
https://gist.github.com/1431722
And my MenuPanel: UIView here:
gist.github.com/1431728
I solved my own question.
I had to tell my UIViewController to ignore touches from any UIToolbar like this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//ignore any touches from a UIToolbar
if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}

Resources