UIImageView, tap and recognition - objective-c

I have an UIImageViews with userInteractionEnabled, and i want to know with image i currently tap. What i should do to know that? I have a poster wall and class for that ThumbPosterModel there i have all poster information with i want to pass through.
So my code is:
for (ThumbPosterModel *tPoster in _thumbsPosterStack) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:tPoster.thumb];
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(i, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height);
[_posterWallScrollView addSubview:imageView];
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[imageView addGestureRecognizer:tap];
}
And then i have:
-(IBAction)imageTapped:(id)sender {
//something do here, but know i don't know what.
}
Should i subclass UIImageView that should contains all ThumbPosterModel params? I don't think it is good idea, but i can't figure any other solution.

Ok this is very simple just add tag proprty of each image with appropriate and distinct value other then 0 and use that proprty to identify the image :)
Happy Coding :)

When the gesture recognizer is fired the sender in the action is the recognizer, so you can get the UIView it is attached to by calling [sender view];
So you could write a method which searched the thumbs poster stack and found the poster with the matching thumb, but it would probably be easier to create a UIImageView subclass which contains a reference to the poster. So when you get the view just do
PosterImageView *selectedPosterView = (PosterImageView *) [sender view];
ThumbPosterModel *selectedModel = selectedPosterView.model;

Set a tag to your each imageView.
Each for loop iteration add your image view to a NSMutableArray (arrayRef).
Accessing the tag of image view when you tap the image view(tagImage) .
then
UIIMageView *imageViewR=[arrayRef objectAtIndex:tagImage-1];
The imageViewR is the tap imageView

Related

How to move an image with a button?

I have an image(clubcow.png) that is passed from an nsarray string to make a picture. Then facedowncow represents the picture. I was wondering how to make a button that will move facedowncow to position (100,100) when button is tapped. Any tips will be appreciated. Also, there is more to this code, I just posted the important parts to give an idea on what is going on.
cardKeys = [[NSArray alloc] initWithObjects:#"clubcow", nil];
currentName = [NSMutableString stringWithFormat:#"%#.png", [cowsShuffled objectAtIndex:currentcow]];
faceDowncow = (UIImageView *)[self.view viewWithTag:1];
faceDowncow.userInteractionEnabled = YES;
I would start by creating a UIButton and adding it to your view controller's view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 20);
[button setTitle:#"Tap Me" forState:UIControlStateNormal];
[button addTarget:self action:#selector(animateImage:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then, link this button to a function that will animate your faceDowncow object. You could add your faceDowncow as a property of the view controller so the following function can easily reference it:
- (void)animateImage:(UIButton *)sender {
[UIView animateWithDuration:0.2
animations:^{
// change origin of frame
faceDowncow.frame = CGRectMake(100, 100, faceDowncow.frame.size.width, faceDowncow.frame.size.height);
} completion:^(BOOL finished){
// do something after animation
}];
}
First of all, it looks like this code is from a view controller subclass, and the cow is a subview. In that case, you should probably have a property for it rather than obtaining it by its tag all the time. If it's instantiated in a storyboard scene/nib then you can hook up an outlet to a property/ivar in your subclass fairly easily.
The easiest way to do what you want is to create the button and use target action so that when it is tapped, it calls a method in your view controller. In the method body, obtain a reference to your cow and set it's frame property, like so:
[faceDowncow setFrame: CGRectMake(100,100,faceDowncow.bounds.size.width,faceDowncow.bounds.size.height)];
If you don't know how target action works, I suggest reading Apple's documentation on the matter. It's as simple as getting a button, calling one method to tell it what events should make a certain method get called, and then implementing that method.

UILabel scaling with UIScrollView

I have a UIScrollView that displays and Image and it scrolls fine and everything. What I want to do is add a UILabel to the UIScrollView to display the title of the image. I managed to do that, but when I zoom out the UILabel does not zoom with the scroll View and stays in the same place on the screen. How would I make it so the label scales with the scrollView Image? Here is the code I have:
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollView.delegate = self;
//This just creates a image from a URL
NSURL * photoURL = [FlickrFetcher urlForPhoto:self.photoCellName format:2];
NSData * photoData = [NSData dataWithContentsOfURL:photoURL];
self.imageView.image = [UIImage imageWithData:photoData];
//Setting up scroll View
self.scrollView.contentSize= self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
NSLog(#"Name = %#", [self.photoCellName valueForKeyPath:#"description._content"]);
//Assigning title to the label
self.textLabel.text = [self.photoCellName objectForKey:#"title"];
Make sure label is the subview of your scrollview.
I'm guessing you are providing the UIImageView as the View that will be zoomed by the UIScrollView by implementing the method in the UIScrollViewDelegate.
If not, I'm not sure how your zooming is working then. If you are providing it, you'll have to return a UIView that contains as subviews your UILabel and your UIImageView and you will have to manually apply transformations to the UIView to resize it.
I guess that a similar question was answered in this SO thread, and Dimme (the one that answered it)provided a complete solution with source code, hope it helps!
You should check if you setup autoresizingMask property of your UILabel
self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
You can do it in IB too. Then if you will change the frame of its superview the frame of your label will be updated during - (void)setNeedsLayout handling process
... and DO NOT block the main thread creating images from url in - (void)viewDidLoad!

how to perform double tap on imageView

I have work on touch event but that would be not work because of I take imageView in ScrollView so,that when I touch the image that directly work scrollview but not work touch on image so give any suggestion and source code which is apply in my code.....
As frauen1 says in their answer here:
1) In the UIScrollView class, set the value of canCancelContentTouches
to NO - this tells the UIScrollView class to allow touches within
subviews (or, in this case, in subviews of subviews).
2) In my "card" class, set exclusiveTouch to YES - this tells the
subview it owns the touches inside of it.
Now it will allow double tap on ImageView with following code
UITapGestureRecognizer *tapRecognizer;
self.imgViewGVC.userInteractionEnabled = YES;
tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTapView:)];
tapRecognizer.numberOfTapsRequired = 2;
[self.ImageView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

Double-tap to zoom in PDF UIScrollView?

I have an "PDF Viewer" in my application. I use the CGPDFDocumentRef and CGPDFPageRef to show the PDF. To zoom in the PDF I use a UIScrollView.
I am already able to pinch-zoom in the UIScrollView but I also want to be able to double-tap to zoom.
Double tap with one finger takes you to different levels in the PDF until you hit the maximumZoomLevel and then be able to double-tap with two fingers to the minimumZoomLevel.
I haven't been able to find a solution on the Internet that shows you this in particular so I'm asking here. How could I accomplish this?
In viewDidLoad:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self.scrollView addGestureRecognizer:doubleTap];
[doubleTap release];
Method:
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{
if(self.scrollView.zoomScale > self.scrollView.minimumZoomScale)
[self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
else
[self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES];
}
I think that you can use UITapGestureRecognizer with numberOfTapsRequired property after that in handle method (which you specify when alloc/init the recognizer) and use to use – zoomToRect:animated: or – setZoomScale:animated: on the view to zoom.
Edit: keep track on current zoom level to reverse the effect.

Touch event of a picture without using UIButton

Is it possible to register a touch of a picture without using UIButton? And if it is, how can i do this?
I can just think of changing the background of a button in the code, and then use IBAction to detect the touch.
Btw, sorry for my english.
Sure. You can put a UITapGestureRecognizer on a UIImageView (remember to set the image view's userInteractionEnabled property to YES):
// assumes "myImageView" is your UIImageView that has user interaction enabled
UITapGestureRecognizer * tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[myImageView addGestureRecognizer:tap];
later in your controller...
- (void)tap:(UITapGestureRecognizer *)recognizer
{
// whatever you want
}
However, you can set up a UIButton to basically be a dumb image. Just set the background image for the normal state, disable all the adjusting on highlighting and touches, and you should have exactly what you're thinking of.
If I've misunderstood what you're getting at, feel free to clarify.