UIButton 优于 UIImageView(优于 UIScrollView)

发布于 2024-11-02 04:46:48 字数 1766 浏览 0 评论 0原文

我想显示一个图像(大于 iPhone 的屏幕),用户可以滚动。 不难,我已经用这段代码完成了: 在我的 .h 文件中

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

在我的 .m 文件中

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];

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

return img;

}

中,一切正常!

现在我想在 UIImage 上添加一个 UIButton,而不是在 UIView 上,因为我希望如果用户缩放或移动图像,uibutton 会跟随 uiimage 的移动/缩放。

所以我添加了这段代码:

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];

但是 UIButton 是“不可点击的”! 当然,UIView 和 UIImageView 已将 UserInteractionEnabled 设置为 YES!

谢谢!

编辑:如果我写

[scroller addSubView:btn];

它可以工作,但是当然,如​​果用户放大/缩小,UIButton 总是大 100x25。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼中杀气 2024-11-09 04:46:48

解决了!
我不知道为什么,但如果我在 Interface Builder 中检查了 UserInteractionEnabled(在 UIImageView 上),它不起作用。
如果我写

img.UserInteractionEnabled = YES;

它就可以了!

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!

欢烬 2024-11-09 04:46:48

我尝试过这个,它对我有用......

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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文