UIButton 优于 UIImageView(优于 UIScrollView)
我想显示一个图像(大于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决了!
我不知道为什么,但如果我在 Interface Builder 中检查了 UserInteractionEnabled(在 UIImageView 上),它不起作用。
如果我写
它就可以了!
SOLVED!
I don't know why, but if i checked
UserInteractionEnabled
(on the UIImageView) in Interface Builder, it doesn't works.If i write
it works!
我尝试过这个,它对我有用......
i tried this and it works for me.......
此代码可能对您有用
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