iPhone SDK - Using touches to delete images - iphone

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];
}

Related

Gesture recognizer does not recognise a view which is created over an UIImageView

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];

Select Image on tapped gesture in iphone

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.

Touch event in app delegate iOS

How to achieve touch even for UIImageView in appDelegate?
You can use:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(anyMethod)];
[imageView addGestureRecognizer:tgr];
[tgr release];
Before doing this, enable user interaction of UIimageView.
This might be helpful for you:
http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/

scrollview's subview does not get touch events when scroll view scrolls

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.

touch events on UIimageView which is on UIwindow

I m using UIimageview on top of UIWindow. I want to handle touch event for that Imageview, but m not able to do it.
When i use UIimageView on UIview, we can use touchBegan, touchEnded but how to handle it when it is on UIWindow instead of UIView.
Thanks..
If you just want to handle the tap, you have to set,
imageView.userInteractionEnabled = YES;
By default UIImageView has the userInteractionEnabled property set to NO. And you can add a tap gesture recognizer to it,
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(anyMethod)];
[imageView addGestureRecognizer:tgr];
[tgr release];
If you want to move the image, you can use UIPanGestureRecognizer.
- (void)sendEvent:(UIEvent *)event
try this function