I need to show an image, and above it some buttons that correspond to certain clickable areas of the image, the problem is that the image has zoom, and when i zoom the image, the buttons do not stay in the same place as the picture. The solution was to use the class ImageScrolView, used in the example of Apple PhotoScroller. The button appears exactly in the right place, regardless of zoom.
Just like the example, i use a ScrollView to display multiple images, these images are shown by class ImageScrollView.
This class declares one UIView in the .H file, but in the implementation file, the method that displays the image, convert the UIView into a UIImageView
imageView = [[UIImageView alloc] initWithImage: image];
then create my button and add to the imageView.
amazon = [UIButton buttonWithType: UIButtonTypeRoundedRect];
amazonia.frame = CGRectMake (92,240,109,142);
[amazon addTarget: self action: # selector (selectState :) forControlEvents: UIControlEventTouchUpInside];
[imageView addSubview: amazon];
My problem is that the button does not respond to button events, it appears in the right place and everything, but does not respond to button events.
Any ideas? Ideias on how to implement those clickable areas on the image are welcome too.
make sure to set imageView.userInteractionEnabled = YES!
Hope that helps.
Related
I was wondering how you would hide an image in Objective-C.
I know you can hide text or a button like this
StartGame.hidden = YES;
But when I try to do this with an image, i get an error.
Since UILabel, UIButton, UIControl and UIImageView are all childs from UIView they all have the hidden property to hide them.
So any instance of the of a view (or its childs) can be hidden by setting the hidden property to YES.
If your UIImageView is part of your viewcontroller it would make sense to make a property of it rather then just an variable, but I can not see your full code or error to say that the error might be found there.
Also property or variables should not start with a capital, its not in line with the Objective-C style.
UIImageView *ImageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y,width, height)];
[ImageView setImage:[UIImage imageNamed:#"image.png"]];
and to hide this image You just need to write
[ImageView setHidden:YES];
in some iPhone apps i saw a button which was floating over the content view, eg. in the app EyeEm. When the user is scrolling the content, the button remains where it is and is still an interaction element.
Ho do I implement this?
My approach would be:
Create a view with content
Put a button on it
But how to make the button floating?
edit:
The floating seems to be the default behavior. Interestingly addSubview and insertSubview have the same behavior when placing the button... both are floating over the content.
- (void)addOverlayButton {
UIButton *oButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[oButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[oButton setTitle:#"Show View" forState:UIControlStateNormal];
oButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:oButton];
//[self.view insertSubview:oButton aboveSubview:_scrollView]; // same result as addSubview.
// Both solutions let the button float over the content.
}
Add a subview to your window's content view with insertSubview:aboveSubview: method with your button and scrollview as arguments, but be careful: if two sibling views both have transparency, the resulting drawing behaviour is undefined.
if you using storyboard just add button over it, like
if you want to do with code insertSubview:buttonView aboveSubview:scrollView
no matters its table or scroll view
I have a UIScrollView, with a UIImageView and UIButton inside of it.
I was reading in the answers here that if I have a single view that contains all of my zoomable content, then I should be fine. The problem is that the button doesn't zoom when the image does.
I thought that what I have would work, since the Scroll View is the zoomable view.
Do I need to create another UIView and put everything in there (and keep the same hierarchy?) ?
Have you checked your Springs and Struts? Look in the size inspector under "autosizing". Those little red lines.. if that doesn't make sense, search for 'auto layout'.
I ended up fixing this by declaring the button in code, and added a UView into the hiearchy. The uiview is a subview of the scroll view, and the imageview is a subview of the uiview. I then added the buttons to the uiview.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
...
[view addSubview:button];
I create some buttons dynamically on my view and i decide their frames according to some of my JSON respone parameters. But i want to autoresize them when the device(dsimulator) rotates. I can easily do this on interface builder but can't do anything on dynamic ones. Can someone help?
EDIT
Here is a snipped of my code
if (button.tag==1) {
button.frame = CGRectMake(30.0f, yPosition, 200.0f, buttonHeight);
}
if (button.tag==2) {
button.frame = CGRectMake(280.0f, yPosition, 200.0f, buttonHeight);
}
if (button.tag==3) {
button.frame = CGRectMake(530.0f, yPosition, 200.0f, buttonHeight);
}
There is no problem when using Portraid mode but when it rotates to Landscape a big empty area stays on the right side of the screen. How can i fix this?(I mean, when i rotate, i want the buttons got to the center of the scren's width)
EDIT:
I played with autoresizing on Size Inspector(Xcode 4.3) with my xib file and it works great, but whatever i did i couldn't resize the dynamically created buttons after rotation. I tried almost all of AutoresizingMask enums of UIView but nothing changes. Can someone please help
You can define by code what was the expected behavior when the device is rotated.
You can take a look at: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/c/tdef/UIViewAutoresizing
You need to set the button behavior when you add it like:
[button setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
The attributes you've set up in your interface builder file are the UIViewAutoresizing attributes.
Take a look at this documentation from Apple on the UIView class (which your button is a subclass of); look for the UIViewAutoresizing attribute. That's the one you'll want.
Update: Here's a snippet of code for an MKMapView that uses this ability:
mainMapView = [[MKMapView alloc] initWithFrame:CGRectMake(20, 239, 280, 122)];
[mainMapView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
With UIViewAutoresizingFlexibleTopMargin, the map view moves down when the user answers the phone --- it fixes the map's position relative to the top of the screen.
Dig around in the documentation and you'll find the autoresizing mask that works best for your situation.
I need to allow the user to add photo either from source type camera or photo library when the user taps on the UIImageview.
I don't want to present a uiimagepicker controller by clicking on a UIButton, I want to allow the user to add it by tapping on the imageview.
How can I achieve this? Should I add the button the imageview, and when a tap occurs, present an action sheet with buttons camera and photo library and when the user selects one of the 2 buttons from action sheet allowing him to add photo to the uiimageview?
You can still use a UIButton, just have that button's view as the image you want.
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];
This will allow you to use an image as your button.
#Amit Shah has the right answer - you should use a button.
But if for some reason you MUST use a UIImageView (and it doesn't sound like you do) - you can subclass UIImageView and override the touchesBegan/touchesEnded methods:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html