Reputation: 2865
How can i select image on tapped gesture. I want to add another image same as I have tapped on . If i have 3 images and if I have tapped 2nd one then it will dynamically add 4th image that will be same as 2nd one.
UIImage *image = [[info objectForKey:@"UIImagePickerControllerOriginalImage"] retain];
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width/4, image.size.height/4)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
-(void)tapped:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
NSLog(@"Tapped");
[self.view bringSubviewToFront:[(UITapGestureRecognizer*)sender view]];
UIImageView *image = (UIImageView *)[(UITapGestureRecognizer*)sender view];
[self.view addSubview:image];
}
I am using [(UITapGestureRecognizer*)sender view]]
to get that view. But I cant. And on tapped I want to add that image which I have tapped.
Upvotes: 1
Views: 3630
Reputation: 8563
You seem to be confusing a UIImage
with a UIImageView
. A UImageView
is a kind of UIView
and can have one (and only one) superview. If you want to know what image it holds you can use the 'image'
property. Then you can create a new UIImageView
with the same UIImage
as the one that tapped. Depending on your application you may need to resize its frame, and set it contentMode
.
In any event, if the tap event is not being registered at all it is likely because UIImageView
are by default userInteractionEnabled=NO
, you must explicitly set it to be YES
either in code, or in the XIB file.
Upvotes: 1
Reputation: 107191
The issue is with your tapped method. When you write (UIImageView *)[(UITapGestureRecognizer*)sender view];
you'll get the same added imageView
and it's frame will be same as when it was added. You are then adding it using addSubview
, If you do so it'll be added at the same position that it was added previously.
Just change your tapped
like:
-(void)tapped:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
UIImageView *imgView = (UIImageView *)[(UITapGestureRecognizer*)sender view];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:yourFrame]; //Set the next frame for the image view
imageView.image = imgView.image;
[self.view addSubview:imageView];
[imageView release];
imageView = nil;
}
Also add the gesture to UIImageView like:
[imageview addGestureRecognizer:tapRecognizer];
not to UIView.
Also set imageview.userInteractionEnabled = YES;
Upvotes: 5
Reputation: 2576
3 Problems:
UIImageView
which holds the same UIImage
, as Jon Rose pointed out.UIImageView
, like Midhun MP pointed out.To solve 3, You either need (a) one recognizer for each UIImageView
, or
(b) you take one recognizer for the containing view, which is self
in your case.
In case (a) you really should subclass UIImageView
and implement the gesture handling there. In case (b), you'd add the recognizer to self
and on the tapped:
event you'd loop over the subviews to find the one that was tapped via
for(UIView* subject in self.subviews)
{
CGPoint pointInSubjectsView = [recognizer locationInView:subject];
BOOL pointInsideObject = [subject pointInside:pointInSubjectsView withEvent:nil];
if(pointInsideObject){
//subject is the tapped view. do the work...
}
}
Your code suggests that you are doing animations which you want to cancel on tap and that the images may overlap and you want to bring the tapped image to the front? If this is the case, you probably should go with (a).
Good luck.
Upvotes: 1