如何在 iPhone 上使用 UIScrollView 来显示重叠图像
你好,我在特定位置有一个大图像和一个小图像...... 我都添加到ScrollView下面了,但是还是不能正常使用? 我想放大,每个都放在它的位置
这是我的代码:
myScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
myScrollView.maximumZoomScale = 3.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
[myScrollView addSubview:imageView];
谢谢
你好,麦克沃斯;我做到了,但是如果我没有最大化图像,我就无法将滚动视图移动到任何地方,但是如果我缩放它,我可以看到其余的..我不知道为什么我尝试更改它,但它工作。你能看一下下面的代码吗?
- (void)viewDidLoad{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"ucd.png"];
//creating a view for UCD image
ucdView = [[UIImageView alloc] initWithImage:image];
//setting the frame for the ucdView as the same size of ucd image
[ucdView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
//for the red pin to display in specific loc
UIImageView *Health = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redPin.png"]];
[Health setCenter:CGPointMake(310,135)];
//adding helathView to ucdview
[ucdView addSubview:Health];
//everything for scroll view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ucdView.frame.size.width, ucdView.frame.size.height)];
[scrollView setMinimumZoomScale:1.0];
[scrollView setMaximumZoomScale:3.0];
scrollView.clipsToBounds = YES;
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
[scrollView setDelegate:self];
[scrollView addSubview:ucdView];//adding ucd view to scroll view
[self.view addSubview:scrollView]; // adding scrollview to self.view main view
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {//apply zooming for scrollview
return scrollView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你实现了吗:
另外,你知道通过将scrollView的contentSize设置为你的imageSize,它不能变得比这更大,对吗?它将放大到位,相应地裁剪外部像素。顺便说一句,如果这是你的计划,为什么不直接设定呢?
=====进一步的答案,响应其他来源
好吧,我注意到的主要事情是您需要在viewForZoomingInScrollView中返回imageView ucdView,而不是scrollView。看起来您已经将 ucdView 创建为属性(以便它与您的视图一样长),但以防万一...
1)在您的 .h 文件中:
2)在您的 .m 文件中;
3) 将您的行:
更改为
4) 将行更改
为:
5) 将以下内容添加到您的 dealloc 例程中:
另外,您是否添加了> ViewController 定义的协议(在 .h 文件中)?
最后,你有滚动视图和健康的泄漏;添加[scrollView释放];以及 viewDidLoad 末尾的 [健康发布]
Did you implement:
Also, you know that by setting the scrollView's contentSize to your imageSize, it can't grow any bigger than that, right? It'll zoom in place, cropping the outer pixels accordingly. As an aside, if that is your plan, why not just set it directly?
=====Further answer, responding to additional source
Well, the main thing I notice is that you need to return the imageView ucdView in viewForZoomingInScrollView, not the scrollView. It looks like you already created ucdView as a property (so that it lives as long as your view does), but just in case...
1) In your .h file:
2) In your .m file;
3) change your line:
to just
4) change the line
to:
5) Add the following to your dealloc routine:
Also, did you add
<UIScrollViewDelegate
> protocol to your ViewController definition (in .h file)?Finally, you have a leak of scrollview and Health; add [scrollView release]; and [Health release] at end of viewDidLoad