tap gesture not recognized on uiimageview - objective-c

I added two uiimageviews, one on another subview uiview (imageview1,imageview2). In the first view the top uiimageview is hidden(imageview2) and in the second view the bottom imageview is hidden(imageview1).
Allocating tap gesture:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
Set user interaction for both uiimageview to YES.
[singleTap setNumberOfTapsRequired:1];
[singleTap1 setNumberOfTapsRequired:1];
// adding gesture to uiimageview
Add tap gesture recognizer and selector respectively.
[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];
But my taps are not recognized.
Can any one tell me where the mistake is?

Try setting setUserInteractionEnabled:YES before adding gesture recognizer.
[imageview1 setUserInteractionEnabled:YES]
[imageview2 setUserInteractionEnabled:YES]
[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];
Update:
After the comment you have made I suggest you bring your views to the top before detecting the tap event. Because parent imageView is above and catches these taps.
[yourparentview bringSubviewToFront:imageview1];
[yourparentview bringSubviewToFront:imageview2];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
singleTap.delegate = self;
[imageview1 addGestureRecogniser:singleTap];
[singleTap1 release];
imageview1.userInteractionEnabled = YES; //disabled by default

Related

iOS - Double tap on uibutton

I have a button and I'm testing the taps on it, with one tap it change a background color, with two taps another color and with three taps another color again.
The code is:
- (IBAction) button
{
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTwice:)];
UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTrice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
tapTrice.numberOfTapsRequired = 3;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[tapTwice requireGestureRecognizerToFail:tapTrice];
//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture
[self.view addGestureRecognizer:tapOnce];
[self.view addGestureRecognizer:tapTwice];
[self.view addGestureRecognizer:tapTrice];
}
- (void)tapOnce:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor redColor];
}
- (void)tapTwice:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor blackColor];
}
- (void)tapTrice:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor yellowColor];
}
The problem is that the first tap don't works, the other yes.
If I use this code without button it works perfectly.
Thanks.
If you want the colors to change on tap of button, you should add these gestures on button in viewDidLoad method or so rather than on the same button action. The above code will repeatedly add gestures on tap of the button to the self.view and not on button.
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTwice:)];
UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTrice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
tapTrice.numberOfTapsRequired = 3;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[tapTwice requireGestureRecognizerToFail:tapTrice];
//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture
[self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
[self.button addGestureRecognizer:tapTwice];
[self.button addGestureRecognizer:tapTrice];
}

how to make a imageview do a segue

I have a bunch of imageview generated in a gridview. But now I want that each imageview can do a segue to another screen. Does anybody knows how I can do that? Here is the code of how I generated my imageview and put it into my grid.
UIImageView * myView = [[UIImageView alloc] initWithImage:myImage];
CGRect rect = myView.frame;
rect.origin.x = xCurrent;
rect.origin.y = yCurrent;
myView.frame = rect;
myView.tag = cellCounter;
[gridContainerView addSubview:myView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
myView.userInteractionEnabled = YES;
[myView addGestureRecognizer:tap];
Please help
Kind regards
You are looking for a tapGestureRecognizer. Add one of those to your image views and make the action call your segue method.
UIImageView *img = [[UIImageView alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySequeMethod:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:tap];
A:
use a custom button instead of UIImageView, and set the image... and then segue with that button, (it looks the same)
B:
use a gesture recognizer to the view, and use a tap with one finger.

Losing Gesture Recognizers in UIPopoverController

I have a class called PhotoBoothController that I use to manipulate an image using gestures. It works fine when I run it on iPhone. However when I run it on iPad, I display the PhotoBoothController inside a UIPopoverController. The image appears fine, however I can't manipulate it as my gestures don't appear to be recognized. I'm not sure how to make the UIPopoverController take control of the gestures.
I present the popovercontroller and configure the view thus:
- (void)presentPhotoBoothForPhoto:(UIImage *)photo button:(UIButton *)button {
//Create a photoBooth and set its contents
PhotoBoothController *photoBoothController = [[PhotoBoothController alloc] init];
photoBoothController.photoImage.image = [button backgroundImageForState:UIControlStateNormal];
//set up all the elements programmatically.
photoBoothController.view.backgroundColor = [UIColor whiteColor];
//Add frame (static)
UIImage *frame = [[UIImage imageNamed:#"HumptyLine1Frame.png"] adjustForResolution];
UIImageView *frameView = [[UIImageView alloc] initWithImage:frame];
frameView.frame = CGRectMake(50, 50, frame.size.width, frame.size.height);
[photoBoothController.view addSubview:frameView];
//Configure image
UIImageView *photoView = [[UIImageView alloc] initWithImage:photo];
photoView.frame = CGRectMake(50, 50, photo.size.width, photo.size.height);
photoBoothController.photoImage = photoView;
//Add canvas
UIView *canvas = [[UIView alloc] initWithFrame:frameView.frame];
photoBoothController.canvas = canvas;
[canvas addSubview:photoView];
[canvas becomeFirstResponder];
[photoBoothController.view addSubview:canvas];
[photoBoothController.view bringSubviewToFront:frameView];
//resize the popover view shown in the current view to the view's size
photoBoothController.contentSizeForViewInPopover = CGSizeMake(frameView.frame.size.width+100, frameView.frame.size.height+400);
self.photoBooth = [[UIPopoverController alloc] initWithContentViewController:photoBoothController];
[self.photoBooth presentPopoverFromRect:button.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
I thought [canvas becomeFirstResponder] might do it but it doesn't appear to make a difference.
Any advice much appreciate, thank you.
UPDATE: adding code as per comment
- (void)viewDidLoad {
[super viewDidLoad];
if (!_marque) {
_marque = [CAShapeLayer layer];
_marque.fillColor = [[UIColor clearColor] CGColor];
_marque.strokeColor = [[UIColor grayColor] CGColor];
_marque.lineWidth = 1.0f;
_marque.lineJoin = kCALineJoinRound;
_marque.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
_marque.bounds = CGRectMake(photoImage.frame.origin.x, photoImage.frame.origin.y, 0, 0);
_marque.position = CGPointMake(photoImage.frame.origin.x + canvas.frame.origin.x, photoImage.frame.origin.y + canvas.frame.origin.y);
}
[[self.view layer] addSublayer:_marque];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self.view addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[canvas addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:1];
[tapProfileImageRecognizer setDelegate:self];
[canvas addGestureRecognizer:tapProfileImageRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && ![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]];
}
I am working on a similar application, and i can easily manipulate images on the canvas.
1) Check if the userInteractionEnabled property of the canvas is set to YES.
2) You can try adding the gestures to the ImageView's rather than the canvas. (I am guessing u want to zoom , rotate n swipe the images).
some guesses:
is this because viewDidLoad method have been called before cavas property been set? In this case, you could try to add recognizers after present popover in another method such as viewDidAppear.
I also agree that the key may be that UIPopoverController allows interaction to other views outside of the popover. So i suggest to move [canvas becomeFirstResponder]; to the end of the viewDidLoad method in PhotoBoothController and try self.view becomeFirstResponder here because you actually add recognizers to self.view.

Adding Tap Gesture on UIImage

I am trying to make clickable UIImage, where the user can click it then it'll animate...
i am working with the UIScrollVIew that's why i used the UITapGesture instead of touchesBegan, and it seems that UIGestureRecognizer is not compatible with UIImage...
am i right?
i keep receiving this error message
receiver type 'UIImage' for instance message does not declare a method
with selector 'addGestureRecognizer'
is there any other way?
You have to add TapGesture in UIImageView not UIImage
imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[imgView addGestureRecognizer:tapGesture1];
[tapGesture1 release];
You can response to the tap with the defined selector and do stuff there
- (void) tapGesture: (id)sender
{
//handle Tap...
}
You have to add the gesture to UIImageView, not UIImage
You can simply add a TapGestureRecognizer to a UIImageView. You have to use a UIImageView because gesture recognizer are only allowed to be added to views.
UIView *someView = [[UIView alloc] initWithFrame:CGRectZero];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
tapRecognizer.numberOfTapsRequired = 1;
[someView addGestureRecognizer:tapRecognizer];
You can response to the tap with the defined selector and do stuff there
- (void)tapAction:(UITapGestureRecognizer *)tap
{
// do stuff
}
Try with UIButton instead of UIIMage and make the UIButton type custom. And on clicking the same you can show the animation.

How to let the pageController control the UIImageView?

I want a UIImageView swipe left, the UIImageView will change the image, at this time, the pageController +1, if swipe right, the pageContoller -1, and display the previous image... ...How can I implement it? Any simple code? thz u.
Just attach a UISwipeGestureRecognizer to the UIImageView. When the gesture recognizer triggers, you would check it's direction and then change images and update the pageController accordingly.
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(next:)];
UISwipeGestureRecognizer *swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftRecognizer];
swipeLeftRecognizer=nil;
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(prev:)];
UISwipeGestureRecognizer *swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
swipeLeftRecognizer=nil;
[recognizer release];