开发者

UIButton over UIImageView (over UIScrollView)

开发者 https://www.devze.com 2023-02-26 19:15 出处:网络
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:

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 = UIViewA开发者_开发百科utoresizingFlexibleHeight | 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.


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!


i tried this and it works for me.......

UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];

UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"]  forState:UIControlStateHighlighted];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
 [    ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];

 [imageView addSubview:ButtonOnImageView];
 [self.view addSubview:imageView];


This Code May be usefull to You

http://developer.apple.com/library/ios/#samplecode/Scrolling/Listings/MyViewController_m.html#//apple_ref/doc/uid/DTS40008023-MyViewController_m-DontLinkElementID_6

0

精彩评论

暂无评论...
验证码 换一张
取 消