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.
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;
3 Problems:
You need a new UIImageView which holds the same UIImage, as Jon Rose pointed out.
You need to set the frame for the new UIImageView, like Midhun MP pointed out.
Both do miss the gesture recognizer logic, which is broken in your case, too. You can add a gesture recognizer to only one view at a time.
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.
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.
Related
I have 2 problem and i am totally confuse. please help me
1) I face one problem that i want to assign click listener to image view which is inside of my subview and that subview is with in cell of UITableView.
In my tableview cell there are two image-view i want to give them click listener and also identify them like which image-view is clicked on which row etc etc.
if i write scrollview.userinteractionEnable=YES; than my didSelectRowAtIndexPath Not responds.
i know its because of subview.
But if i change scrollview.userinteractionEnable=NO; than my didselectrowatIndexpath code executes. but scrollview does not scroll to horizontal..
What do i do ? I want horizontal scroll plus Image-view click Listener on both ImageView.
** Solve click problem by crating scrollview sub class**
This is my Customcell class
cellScrollViewClass *scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0.0,0.0,430,187)];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3,scrollView.frame.size.height);
scrollView.scrollEnabled=YES;
scrollView.userInteractionEnabled=YES;
for (int i = 0; i < 3; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
if(i==2) // to check Last item must be train Engine
{
UIView *EngineView = [[UIView alloc]initWithFrame:frame];
UIImageView *engineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-112.5, 6, 430, 180)];
UIImage *Image = [UIImage imageNamed:#"backengine.png"];
engineImageView.contentMode = UIViewContentModeScaleAspectFit;
engineImageView.image = Image;
[EngineView addSubview:engineImageView];
[scrollView addSubview:EngineView]; // add 3rd imageview that is Train Engine to end of scrollview
}
else{ // Not equal to 2 than it must be wagon of train.
UIView *wagon = [[UIView alloc]initWithFrame:frame];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"wagon.png"]];
wagon.backgroundColor = background;
UIView *videoviewContainer = [[UIView alloc]initWithFrame:CGRectMake(20, 40, 220 , 200)];
UIImageView *videoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 220, 100)];
UIImage *bgImage = [UIImage imageNamed:#"video.png"];
videoImageView.image = bgImage;
videoviewContainer.contentMode = UIViewContentModeLeft; // set Video Image view to left hand side of wagon UIView
videoImageView.tag=2;
[videoviewContainer addSubview:videoImageView];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[videoImageView addGestureRecognizer:singleTap];
[videoImageView setMultipleTouchEnabled:YES];
[videoImageView setUserInteractionEnabled:YES];
UIView *textUiView = [[UIView alloc]initWithFrame:CGRectMake(238,28, 150 , 187)];
textUiView.contentMode = UIViewContentModeRight;
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(28,10, 150 , 87)];
label.text=#"This is testing text for IOS app to check line are aligned or not and to check video image and nested views for UIviews";
label.backgroundColor = [UIColor clearColor];
label.textColor=[UIColor redColor];
label.numberOfLines = 4;
[textUiView addSubview:label];
[wagon addSubview:textUiView];
[wagon addSubview:videoviewContainer];
[scrollView addSubview:wagon];
[self.contentView addSubview:scrollView];
}
}
}
return self;
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
NSLog(#"Touch event on view %d",gesture.view.tag);
}
And I created Scrollview subclass like this..
#implementation cellScrollViewClass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"You are in scrollview");
}
return self;
}
IS this one is right method of Implementation?
Any Help is Appreciated . Thank you in advance.
Use UITapGestureRecognizer. Create and attach a tap gesture recogniser to each image view when you create it. Also set the tag of the image view when you create it so you can identify which one it is on the cell.
When you get the callback from the recogniser you can get the tag by:
recogniser.view.tag
And you can get the table view cell by looping on the superview until you find a table view cell class:
UIView *superView = recogniser.view.superview;
while (superView != nil && ![superView isKindOfClass:[UITableViewCell class]]) {
superView = superView.superview;
}
Then you can get the index path from the table view using
[tableView indexPathForCell:superView];
To get the on click listener for UIImageView have a category of it & use touches begin method and delegate callback to 'tableViewController'.
Use tags to identify which image is clicked & which row.
To avoid overlap, if the cell us already allocated just remove the previous images to do this have a else condition.
you need to implement the UIScrollView delegate callbacks and check what kind of scroll the user is doing (horizontal scroll or vertical scroll) and then changing the tableview or the scrollview offset manually, thats pretty much the only way i see this...
as for your image you need to set a UIButton for everyImage and link them to a selector, then you can use the tag of the button to know what image it was.
you can add a custom button over the imageview with same frame of image view and the you can add the selector on the button.
For handling gestures on UIImageView make sure that the UIImageView that you are using has the userInteractionEnable = YES by default is set to NO
I have an UIImageview with tap gesture on it, such that any tap over it creates a rectangular view with white background. Which I can move any where over imageview. Like this I can create infinite views over that imageview.
Now my problem is that I want to add double tap gestures to newly created views but when I click on any of those views then a new view is created over that view.
But what I want is that if I tap on a view then no new view must be created over that view.
Currently gestureRecognizer recognises UIImageView even if I have created a new view over UIImageView.
ImageView,
UIImageView *imageView = [[UIImageView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(createNewView)];
[tapRecognizer setNumberOfTapsRequired:1]; // allow to create new view
UITapGestureRecognizer * tapRecognizerToFail = [[UITapGestureRecognizer alloc]init];
[tapRecognizer setNumberOfTapsRequired:2]; // to disallow more than 1 tap.
[tapRecognizer requireGestureRecognizerToFail:tapRecognizerToFail];
[imageView setGestureRecognizers:[NSArray arrayWithObjects:tapRecognizer,tapRecognizerToFail,nil]];
// New View Creation over ImageView
-(void)createNewView
{
UIView * view = [[UIView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(newViewAction)];
[tapRecognizer setNumberOfTapsRequired:2];
[view addGestureRecognizer:tapRecognizer];
[imageView addSubview:view];
}
// Double tap action over newly created view
-(void)newViewAction
{
// goes here
}
You can use requireGestureRecognizerToFail: to declare a dependency between your two recognizers.
[secondaryGesture requireGestureRecognizerToFail:primaryGesture];
how can you delete an image by using touches in the iphone sdk? Im making a game with images so i was wondering if there is a way were if you touch an image it gets deleted from the view.
Yes. You can add UITapGestureRecognizer to your image view, and in the recognizer action you can get the image view by using recognizer.view propery. And you can delete/remove it from its super view by using [image removeFromSuperview] method.
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onImageTapped:)];
[imageView addGestureRecognizer:tgr];
[tgr release];
And the onImageTapped: method looks like this,
- (void)onImageTapped:(UITapGestureRecognizer*)recognizer {
UIImageView *imgView = (UIImageView*) recognizer.view;
[imgView removeFromSuperview];
}
I have a scrollView onto which some imageViews are added.
I want that when scrollView scrolls then also the imageViews should get touch events.
But it is not happening in my case.
Only one of the two things happen at a time.
Either the scrollView scrolls but the imageViews do not get touch events
or the imageViews get touch events and the scrollView does not get scrolled.
Can't both of the things happen simultaneously.
Please suggest.
You can use "Tap Gesture Recognizer" to get the events on your images while it's being added to the scroll view.
EDIT
You can refer to this code:
-(void)yourGestureRecognizer
{
UITapGestureRecognizer *yourTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[yourImageView addGestureRecognizer:yourGestureRecognizer];
[yourGestureRecognizer release];
}
You can handle tap here:
-(void)handleTap:(UIGestureRecognizer *)sender{
UIImageView *imageView = (UIImageView *)sender.view;
switch (imageView.tag)
{
case 1:
m_pYourInstance=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:XXXXX animated:YES];
break;
}
Hope this helps !!
I know this was posted a while ago, but...
By default, UIImageView objects have their property userInteractionEnabled set to NO. This could be why touches aren't registering. This has tripped me up on more than one occasion and it's one of those little things that only experience will help.
Set it to YES and see if that fixes it.
Use UIGestureRecognizer. Add the recognizer to your image views.
And if you are targeting some old iOS version, have a look at this link.
yes you can do both. you need to customize your uiimageview by sub classing it and have to add Tap Gesture Recognizer like :
in .h file
#interface CustomView : UIImageView
{
// your variables
}
in .m file
//when you create an instants of your image view UITapGestureRecognizer will added in all your instants.
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// add Gesture for tapping
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
}
return self;
}
then add delegate methods in .m file too
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
}
now in your custom cell, add your custom image view with Tap Gesture Recognizer
hope it will help you...
Use UIButtons with background images set on them. Here's the loop you use to layout UIScrollView subviews:
for (int i = 0; i < [images count]; i++) {
CGRect aFrame = ...;
UIButton *button = [[[UIButton alloc] initWithFrame:aFrame] autorelease];
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
And here you process the touch event:
- (void)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
[self performActionWithImageAtIndex:[images objectAtIndex:button.tag]];
}
That will omit the need to create additional gesture recognizers and will lower the amount of boilerplate work.
-- edit
The other reason your UIImageViews are not detecting the touches is because you create them in code and forget to set userInteractionEnabled to YES.
I am just getting stated with iPhone development and can't seem to find the answer I am looking for what I want to do.
It seems like I should be able to programmatically create a UIImageView and then set up an event handler for it's touch functions.
in c# i would have something that looks like
Button b = new Button();
b.Click+= my handler code
right now I have this
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 141.0f, 151.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
myImage.userInteractionEnabled = YES;
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
What do I need to do to override the touch events?
thanks
While it is not the complete answer to your question, i think the following solution might be better then doing the "invisible-button-workaround"..
simply subclass from UIImageView and override the following method:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//your UIImageView has been touched :)
//event -> "A UIEvent object representing the event to which the touches belong."
//touches -> "A set of UITouch instances in the event represented by event that represent the touches in the UITouchPhaseEnded phase."
}
hope this helps...
I have a mainview with some icons like "about" to open an about view.
In the corresponding controller (mainview) I added an event handler for touchesBegan.
Moreover I need to check which view has been "touched", as there are more icons on the view.
My UIImageView instance is "aboutImg" and user interaction needs to be checked (i.e. UI builder -> attributes inspector of view).
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == aboutImg){
[self ShowAbout];
}
}
Make sure you check the "User Interactions enabled". This is the easiest solution
Just use a custom UIButton with the image as background image.
I've not found a way to do it directly with UIImageView. But if you subclass it and override the nextResponder method to return a delegate under your control you can do what you're after. Expose the delegate as a property of the subclass and then you can pass in a delegate for the touch events and change it at any time.
I've seen other answers suggest putting an invisible button underneath the UIImageView, setting the image view to userInteractionEnabled: NO, and letting the touches pass through to the button.
You need nothing to do with UIImageView, You can use UIButton with type custom and add touch up inside action to it...set button.imageview setImage:
i-e:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100); //edit for you own frame
UIImage *img = [UIImage imageNamed:#"myimage.png"];
[button setImage:img forState:UIControlStateNormal];
If you want to go with UIImageView, then you need to add gestures to your imageview. else you can subclass your UIImageView and then use touch events.
Easiest way to add an onClick event to an UIImageView or to a UIView is to add a tap gesture recognizer to it.
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: #selector(onImageViewClicked)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.myImageView addGestureRecognizer:singleTap];
[self.myImageView setUserInteractionEnabled:YES];