How to detect tapgesture on UIImage? - uiimageview

My application require touch events only on UIImage not on UIImageVIew , How to discard the taps of UIImageView and only accept if tap gesture is recognized on UIImage part .
Thanks, In advance

You cannot add a tapgesture to UIImage. it can only be added to the UIImageview.

Related

Two UIImage in one collectionview cell?

I want to put second UIImage inside collectionview cell but it return error
"error: Illegal Configuration: The newIcon outlet from the DashboardViewController to the UIImageView is invalid. Outlets cannot be connected to repeating content.
"
Here is the picture to describe error that is generated :
How to make two UIImage inside one collectionview cell?
You'll need to add a new UIView into "dashboardCell". Once you've added a UIView you can then add more UIImages to that UIView.

UIScrollView subviews and setNeedsDisplay

I've the following problem :
I have a UIView containing a UIScrollView as a subview. (nib file).
Programmatically I add several subviews (UIImageView) to the UIScrollView, each UIImageview contains an image loaded from the net asynchronously, so I need to update the scrollView when the images are downloaded. In the class responsible of the images fetching, I advertise the the View controller responsible to manage the scrollView, using this code
[[(MosaicViewController *)data] scrollView setNeedsDisplay];
the Ivar data is a pointer to the ViewController.
This stuff don't work,no reload of the scrollView happen
To be sure that the call is triggered I wrote a method inside the viewController containing the scrollView, and inside this method I called setNeedsDisplay,
[(MosaicViewController *)data updateView];
-(void) updateView
{
NSLog(#"setNeedsDisplay");
[self.scrollView setNeedsDisplay];
}
the method updateView is triggered correctly, I mean is called after each Image is downloaded, but the scrollView contents isn't updated. In the ViewController containing the scrollView I don't implement the drawRect method, could be this the reason for the lack of update after calling setNeedsDisplay?
Any help/suggestion/reference etc.. is welcome
Thanks in advance
Dude are you doing imageView.image = downloadedImage ??
Also you should not have to do [self.scrollView setNeedsDisplay] since you did not change anything on scrollView !!
What changed is the content of the imageView and imageView.image = downloadedImage will automatically trigger setNeedsDisplay on imageView !!
Some other check points
Is scrollView visible?
Is scrollView.contentSize set??
Is the scrollView frame correct ??

How to set up background image of the UIView?

In a case of a custom view, the only way i can think of is to lay an UIImageView on top is it and set it's background to some image.
Can image be overlaid onto the regular UIView?
I prefer to place an UIImageView covering whole UIView and arrange it to back in Interface Builder. I think it is the most convenient way to do it.
You can add image as background with image pattern. Use the following line of code.
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"YouImageName.png"]]];
In case you are not applying it to self.view, but to some other view in you xib file, then you need to make it IBOutlet first.

adding tap gesture to a tabBarController

I have a question on how to add a tap gesture to a UITabBarController. As the UITabBarController already has tap gestures built-in (responding to the tapping of the tab bar items on the tab bar), while technically I can add my own gesture to the tabBarController, the tabBar loses its own native tap gesture. Below is what I am trying to do:
UIViewController *VC1 = ....;
UIViewController *VC2 = ....;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects: VC1, VC2, nil];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]
initWithTarget:VC1
action:#selector(tap:)];
[tabBarController.view addGestureRecognizer:tapGR];
This correctly responds to the tapping method "tap: ", but the tabBarController loses its native tapping responses to the tap bar items. I tried to add the gesture to one of the view controllers in the tabBarController like this:
[VC1.view addGestureRecognizer:tapGR];
but then doing it this way the tapping gesture is not recognized at all, although the tabBar's native tap recognition of the tapping on the tab bar items is retained.
Does anyone has any suggestions on how to resolve this type of issues? I guess one way is to pick another gesture other than tapping to go with tabBarController, but I'd really rather not do that....
Much thanks for viewing!
I have to wonder what exactly you're trying to do with taps on a control that already handles taps. Do consider whether what you're doing is going to confuse your users.
But if you must, try setting cancelsTouchesInView to NO on the gesture recognizer. That should allow the touches to be passed on to the view in addition to being processed by your recognizer.

Apple Interface Builder: adding subview to UIImageView

I created UIImageView with the help of Interface Bulder. Now I want to place label inside it (as its subview). In code I can type something like: [myUIImageView addSubview:myUILabel]; But can I do it with the help of IB? I found the solution for UIView, but can't find something similar for UIImageView.
You cannot add a subview to UIImageView in interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then, the overhead of setting autoresizing masks and placements of subviews should all be handled in code, which is cumbersome.
So there is an easy workaround. Instead of dragging an instance of UIImageView in the nib, just drag a UIView and change its class to UIImageView from UIView (cmd+4 option of inspector). The only difference you find in the nib for default imageView instance and your new UIImageView subclass instance is: you cannot set image to your new imageView from nib (cmd+1 option). So, in the -viewDidLoad method of its appropriate viewController, set image to this outlet of UIImageView.
By doing so, you are free to add subviews to your "now UIImageView" instances in interface builder, which is much easy.
I would like to add answer.
While it sucks you cannot add subview to UIImageView, you can get the same effect by incorporating UIView with transparent (clear color) background.
Then put the UIImageview BEFORE it.
So the UIView has the subviews and the UIImageview is the background of the UIView.
I think that's apple's intent.
Here is a screenshot:
Here is the result:
Don't forget to set background as clear color
Now if someone could actually point me to a tutorial how to do this it'll be great. I spent hours doing it the checked answered way. The checked answer is a fine answer but very unintuitive because you can't see your background image clearly while working. I think mine is the proper way to do so.
In latest XCode(4.5) there is an option to drag and drop the required controls to the parent.
It is quite easy.
Attached screen shot for the same. I dragged the Label/TextField and Button to UIImageView
Use this code:
UIImage *image = [UIImage imageNamed:#"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view insertSubview:imageView atIndex:0];
(replace background.png with image) (replace atIndex:0 with whatever place in the .index root you want to insert the image and your off.

Resources