如何在 iPhone 上使用 UIScrollView 来显示重叠图像

发布于 2024-11-17 21:51:18 字数 1858 浏览 4 评论 0 原文

你好,我在特定位置有一个大图像和一个小图像...... 我都添加到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;
}    

Hi, I have a big image and small image over it at a specific location...
I added them all under ScrollView, but it didn't work properly?
I want to zoom in, all together each in its place

Here is my code:

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

thanks


Hi, mackworth; I did it, but if I didn't maximize the image, I can't move the scroll view anywhere, but if I zoomed it, I can see the rest.. I don't know why I tried changing it, but it work. Can you please look at the following code?

- (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 技术交流群。

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

发布评论

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

评论(1

眼泪也成诗 2024-11-24 21:51:18

你实现了吗:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

另外,你知道通过将scrollView的contentSize设置为你的imageSize,它不能变得比这更大,对吗?它将放大到位,相应地裁剪外部像素。顺便说一句,如果这是你的计划,为什么不直接设定呢?

myScrollView.contentSize = imageView.frame.size;

=====进一步的答案,响应其他来源

好吧,我注意到的主要事情是您需要在viewForZoomingInScrollView中返回imageView ucdView,而不是scrollView。看起来您已经将 ucdView 创建为属性(以便它与您的视图一样长),但以防万一...

1)在您的 .h 文件中:

UIImageView * ucdView;
@property (nonatomic, retain) UIImageView * ucdView;

2)在您的 .m 文件中;

@synthesize ucdView;

3) 将您的行:

ucdView = [[UIImageView alloc] initWithImage:image];

更改为

 self.ucdView = [[UIImageView alloc] initWithImage:image];

4) 将行更改

 return scrollView

为:

 return self.ucdView;

5) 将以下内容添加到您的 dealloc 例程中:

self.ucdView = nil;

另外,您是否添加了 > ViewController 定义的协议(在 .h 文件中)?

最后,你有滚动视图和健康的泄漏;添加[scrollView释放];以及 viewDidLoad 末尾的 [健康发布]

Did you implement:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

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?

myScrollView.contentSize = imageView.frame.size;

=====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:

UIImageView * ucdView;
@property (nonatomic, retain) UIImageView * ucdView;

2) In your .m file;

@synthesize ucdView;

3) change your line:

ucdView = [[UIImageView alloc] initWithImage:image];

to just

 self.ucdView = [[UIImageView alloc] initWithImage:image];

4) change the line

 return scrollView

to:

 return self.ucdView;

5) Add the following to your dealloc routine:

self.ucdView = nil;

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

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