Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton over UIImageView (over UIScrollView)

I want to show an image (larger then the screen of the iphone), that the user can scroll. Isn't difficoult, i've done with this code: in my .h file

@interface mappa1 : UIViewController <UIScrollViewDelegate> {
IBOutlet UIImageView *img;
IBOutlet UIScrollView *scroller;
 }

in my .m file

UIImage *image = [UIImage imageNamed:@"prova.jpg"];
img = [[UIImageView alloc] initWithImage:image];
scroller.delegate = self;
scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scroller.contentSize = img.frame.size;
scroller.scrollEnabled = YES;
scroller.directionalLockEnabled = NO;
scroller.userInteractionEnabled = YES;
CGSize ivsize = img.frame.size;
CGSize ssize = scroller.frame.size;

float scalex = ssize.width / ivsize.width;
float scaley = ssize.height / ivsize.height;
scroller.maximumZoomScale = 1.0;
scroller.minimumZoomScale = fmin(1.0, fmax(scalex, scaley));
scroller.zoomScale = fmin(1.0, fmax(scalex, scaley));
[scroller addSubview:img];

and

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

return img;

}

and all works fine!

Now i want to add an UIButton on the UIImage, not on the UIView, because i want that if the user zooms or moves the image, the uibutton follow the movement/zoom of the uiimage.

So i add this code:

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
[btn setTitle:@"Play" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClick)  forControlEvents:UIControlEventTouchUpInside];
[img addSubview:btn];

but the UIButton is "not-clickable"! Of course, the UIView and the UIImageView has UserInteractionEnabled set to YES!

Thanks!

EDIT: if i write

[scroller addSubView:btn];

it works but, of course, if the user zoom in/out, the UIButton is always big 100x25.

like image 298
JAA Avatar asked Mar 20 '26 04:03

JAA


1 Answers

SOLVED! I don't know why, but if i checked UserInteractionEnabled (on the UIImageView) in Interface Builder, it doesn't works. If i write

img.UserInteractionEnabled = YES;

it works!

like image 127
JAA Avatar answered Mar 21 '26 19:03

JAA