UIImageView is not Recognizing Second time Gesture - iphone

I have implemented UITapGestureRecognizer on UIImageView, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE, I am not getting the Tap event of that UIImageView.
Following is the code I am using :
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:#"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(#"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(#"%#", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(#"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:#"dog" withDuration:(12) ofType:#"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:#selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(#"%#",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}

When view is hidden or its alpha component is zero that view won't receive any UIGestureRecognizers.
I can suggest to use next approach if you need to hide some view (let's name it touchableView) but want it to respond to gestures:
Create backgroundView with the same frame as touchableView:
UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
Set background color of backgroundView to clearColor:
backgroundView.backgroundColor = [UIColor clearColor];
Reset position of touchableView:
CGRect frame = touchableView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
Disable user interaction of touchableView:
touchableView.userInteractionEnabled = NO;
Add touchableView as subview to backgroundView:
[backgroundView addSubview:touchableView];
Add appropriate gesture recognizers to backgroundView.
Add backgroundView to view that you want.
Now you can hide touchableView but you will still receive gesture recognizers.
I don't test this but I think it should work.

sure
when UIImageView is hidden. it does not receive any touch events
set alpha zero for uiimageview

Related

UITapGestureRecognizer for UIImageView on UIScrollView never called iOS5

I am adding a UIImageView as a subview to a UIScrollView then i set the image.
Im trying to to use UITapGestureRecognizer.
The selector of the UITapGestureRecognizer is never called in iOS 5 (in iOS 6 it DOES!).
Tried many variations. this is my code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(index*(IMAGE_WIDTH+10), 10.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
[imgView setImageWithURL:[NSURL URLWithString:meal.RecommendedImageURL] placeholderImage:[UIImage imageNamed:#""]];
imgView.layer.cornerRadius = 4;
[imgView.layer setMasksToBounds:YES];
[imgView setUserInteractionEnabled:YES];
[imgView setMultipleTouchEnabled:YES];
[scrollView addSubview:imgView];
if (IOS_NEWER_OR_EQUAL_TO_5)
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[imgView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
this is my selector which is only called in iOS5:
- (void) imageTapped: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
UIImageView *tappedImageView = (UIImageView*)recognizer.view;
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = tappedImageView;
vc.liftedImageView.contentMode = UIViewContentModeScaleAspectFit;
if (IOS_NEWER_OR_EQUAL_TO_5) {
[self.parentViewController presentViewController:vc animated:YES completion:nil];
}
else
{
[self.parentViewController presentModalViewController:vc animated:YES];
}
}
In addition to that, i tried to setCancelsTouchesInView to YES in my UIScrollView but it doesn't work either.
Thanks for your help!
try to add Gesture in Scrollview then check iskind of class image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scrollview addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if ([touch.view isKindOfClass:[UIImageView class]]) {
// we touched a button, slider, or other UIControl
return YES;
}return NO;
}
Implement the 3 methods of UIGestureRecognizerDelegate & return default values:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Ok i solved it very EASILY!
the problem was setting the delegate of the UITapGestureRecognizer to self.
when i removed the delegate = self, it started working :)
Thanks for your assistance

UITapGestureRecognizer not triggering on UIImageView created programmatically

I'm using XCode 4.5.1 .
I added <UIGestureRecognizerDelegate> in .h file and in the start of .m file
#interface FirstViewController ()
#property (nonatomic, strong) UIImageView *img1;
#end
Then in viewDidLoad, creating UIImageView programmatically like
self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, -30, 44, 44)];
[self.img1 setImage:[UIImage imageNamed:#"image1.png"]];
[self.img1 setAlpha:0.8];
[self.img1 setHidden:NO];
[self.img1 setUserInteractionEnabled:YES];
[self.img1 setTag:901];
And then
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
tap.delegate = self;
[self.img1 addGestureRecognizer:tap];
[self.view addSubview:self.img1];
[self.view bringSubviewToFront:self.img1];
and at the end
- (void)guessTapObject:(UITapGestureRecognizer *) gesture
{
// guessing the image
UIImageView *tapImageView = (UIImageView*) gesture.view;
NSLog(#"Gesture Tag: %d", tapImageView.tag);
}
I have 4 different images and creating them programmatically as described above. And applying the animation to move them from top to bottom.
I want that when user tap on the image, it should animate and disappear(i know that code). But the code is not triggering the tap event. I put the breakpoint on the start of the guessTapObject function, but don't go there ever.
At UITapGestureRecognizer declaration, debugging shows that guessTapObject is attached to the recognizer to perform the tap action.
Please help why tap event is not triggering its selector method???
Thanks for your help in advance.
Edited:
i also tried with for loop to have gesture recognizer separately for every imageview
for (UIView * view in self.view.subviews) {
if (view.tag > 900){
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
// tap.delegate = self;
[view addGestureRecognizer:tap];
NSLog(#"tapView: %#", tap.view);
NSLog(#"tap: %i", tap.enabled);
}
}
I was stuck in similar issue.. Make sure imageView is set true for userInteraction..
imageView.userInteractionEnabled = YES;
It worked for me!
Check this
Add following code to your View Controller
In viewController.h file
#property (nonatomic, strong) UIImageView *img1;
#property (nonatomic, strong) UIImageView *img2;
#property (nonatomic, strong) UIImageView *img3;
In viewController.m file
Inside viewDidLoad Method
// Init Image 1
self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 30, 44, 44)];
[self.img1 setImage:[UIImage imageNamed:#"image1_name.jpg"]];
[self.img1 setAlpha:0.8];
[self.img1 setHidden:NO];
[self.img1 setUserInteractionEnabled:YES];
[self.img1 setTag:901];
// Init Image 2
self.img2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 30, 44, 44)];
[self.img2 setImage:[UIImage imageNamed:#"image2_name.jpg"]];
[self.img2 setAlpha:0.8];
[self.img2 setHidden:NO];
[self.img2 setUserInteractionEnabled:YES];
[self.img2 setTag:902];
// Init Image 3
self.img3 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 130, 44, 44)];
[self.img3 setImage:[UIImage imageNamed:#"image3_name.jpg"]];
[self.img3 setAlpha:0.8];
[self.img3 setHidden:NO];
[self.img3 setUserInteractionEnabled:YES];
[self.img3 setTag:903];
// Add Images to view
[self.view addSubview:self.img1];
[self.view bringSubviewToFront:self.img1];
[self.view addSubview:self.img2];
[self.view bringSubviewToFront:self.img2];
[self.view addSubview:self.img3];
[self.view bringSubviewToFront:self.img3];
// Set Tap Gesture to each image of view
for (UIView * view in self.view.subviews) {
if (view.tag > 900){
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tap];
NSLog(#"tapView: %#", tap.view);
NSLog(#"tap: %i", tap.enabled);
}
}
Make sure images should be added before for loop(adding TapGesture using array of subview)
i solved by applying gesture on view, instead of each image view separately.
[self.view addGestureRecognizer:tap];
and then in handler/selector guessTapObject function, i used this
CGPoint tapLocation = [gesture locationInView:self.view]; // gives the tap coordinates
for (UIView * view in self.view.subviews) { // gives view by iterating on each subview
CGRect dropRect = [[[view layer] presentationLayer] frame]; // specific way of getting the frame with respect to its layer
if(CGRectContainsPoint(dropRect, tapLocation)){ // checks for tap in the boundaries of view frame
if (view.tag > 900) // my specific view
// done what i want to do with that specific one
// as i removed it from my superview
}
}
}

View transparency and Gesture handling

I am currently trying to create a View that would handle all the Gesture that could happened on my application.
I would like this view transparent in order to put other view below and they would still display (I don't want to make them subbiews of the handling view)
The Gesture handling works fine until I set the view color on "clearColor" from then, it is has the view disappear. Unless I stick subviews, in this case the gesture are only happening when hitting on subviews.
My question hence is: How could I manage to have the Gesture event happening on a transparent view?
Try something like this. This code adds two subviews to the main view "bottomView which is has a red background and then "testView" which is transparent an overlay on top of "bottomView" with a tap gesture recognizer. If you tap anyway in the "testView" it will print out the NSLog message. I hope that helps.
-(void)viewDidLoad
{
[super viewDidLoad];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[bottomView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:bottomView];
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 150)];
[testView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:testView];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:testView];
[testView addGestureRecognizer:tapRecognizer];
}
-(void)handleTap:(UITapGestureRecognizer *)sender
{
NSLog(#"Tapped Subview");
}

Draw Line with gesture

I add a view as sub view using the following code
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:cppobject->OutputImage];
imageview.contentMode = UIViewContentModeScaleAspectFit;
[holderView addSubview:imageview];
holderView.contentMode = UIViewContentModeScaleAspectFit ;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
[panRecognizer release];
-(void)scale:(id)sender {
}
-(void)rotate:(id)sender {
}
-(void)move:(id)sender {
}
-(void)tapped:(id)sender {
}
I need to draw line when the user use his two fingers to pan and a uilabel (the green one) like in the following image
I had look on How to draw graphics in iphone gesture?
But I couldn't apply it on the subview , specially the DrawInRect method
in panRecognizer function (move) I want to draw line using
UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ;
NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]];
[Points addObject:value];
[holderView setNeedsDisplay];
NSLog(#"End of measuring") ;
and I will use the points in Points to draw line above all the subviews in
-(void)drawRect:(CGRect)rect {
NSLog(#"Entered Draw In Rect");
if (Measuring) {
[[UIColor redColor] setStroke];
UIBezierPath *pathToDraw = [UIBezierPath bezierPath];
for (int n = 1; n < [Points count] - 1 ; n++) {
NSValue * value = [Points objectAtIndex:(NSInteger)n];
CGPoint point = [value CGPointValue];
[pathToDraw moveToPoint:point];
value = [Points objectAtIndex:(NSInteger)n+1];
point = [value CGPointValue];
[pathToDraw addLineToPoint:point];
}
[pathToDraw stroke];
}
}
the problem is [holderView setNeedsDisplay]; never call or fire drawRect any suggestion or help regarding that
any suggestion
The image view being the subview draws on top of the holder view. The imageview is opaque. This means that when the views are drawn there is no part of the holder view that is actually visible, so it's drawRect call is optimised out.
Try ordering the views the other way around, so that the holder view is the subview of the imageview. Then the imageview will draw and the holder view will draw on top of it.
Also, note that you should use the bounds of the parent view as the frame of the subview.
UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]];
Edit (add):
See http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1
, specifically under the section "View Hierarchies and Subview Management":
"Visually, the content of a subview obscures all or part of the content of its parent view"
So, try make the imageview the parent, do your initialisation like so:
// instance variables:
UIImageView* imageView;
MyHolderView* holderView;
imageView = [[UIImageView alloc] initWithFrame:mainRect];
holderView = [[MyHolderView alloc] initWithFrame:[imageView bounds]];
holderView.opaque = NO;
holderView.backgroundColor = [UIColor clearColor];
[imageView addSubview:holderView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:holderView action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
// etc...
Now, the image view is drawn, and the holder view, its subview is drawn on top of it. Now when you call setNeedsDisplay on the holderview it will receive a drawRect: call.
For example, track the gesture like so. This could be in your view controller or in your MyHolderView view subclass; the example here would be in the MyHolderView class so that the location1 and location2 instance variables can be shared easily with the drawRect: method.:
-(void)scale:(id)sender {
if (sender == pinchRecognizer) { // this allows the responder to work with multiple gestures if required
// get position of touches, for example:
NSUInteger num_touches = [pinchRecognizer numberOfTouches];
// save locations to some instance variables, like `CGPoint location1, location2;`
if (num_touches >= 1) {
location1 = [pinchRecognizer locationOfTouch:0 inView:holderView];
}
if (num_touches >= 2) {
location2 = [pinchRecognizer locationOfTouch:1 inView:holderView];
}
// tell the view to redraw.
[holderView setNeedsDisplay];
}
}
and then in the holder view drawRect routine:
-(void)drawRect:(CGRect)rect {
// use instance variables location1 and location2 to draw the line.
}

Show UIButton when Image is touched?

is there any way to hide an UIButton until the UIImageView is pressed??
When the picture is pressed I need to show the back Button, like it works at the Photo App on the iPhone???
Here is the code of my UIButton:
- (void)viewDidLoad {
[super viewDidLoad];
[self ladeImage];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(goToViewA) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
First step : btn.hidden = YES
Then you have to subclass the UIImageView to react to its touchesEnded: event and change the hidden property of your button there. For that, the proper way is to create a protocol (with a viewTouched method). Implement that protocol in the viewController containing your button and you ImageView. Add a delegate propery to the subclassed ImageView (i.e. id<MyCustomProtocol> _delagate;) and assign the view controller to this propery.
btn.hidden = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image name"]];
imageView.userInteractionEnabled = YES; // here to enable touch event
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGestureRecongizer:)]; // handleTapGestureRecongizer is method will call when tap even fire
[imageView addGestureRecognizer:tap]; // Add Tap gesture recognizer to image view
[tap release], tap = nil;
[self.view addSubview:imageView];
[imageView release], imageView = nil;
Method handlerTapGestureRecognizer:
- (void)handleTapGestureRecongizer:(UITapGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
btn.hidden = NO;
}
}
have fun!