放大 UIScrollView 不起作用

发布于 2024-11-28 23:48:53 字数 1829 浏览 1 评论 0原文

有人可以帮我用 UIScrollView 进行缩放吗?

我正在尝试添加从 URL 检索到的三个图像并使用 UIScrollView 显示它。我可以做显示部分,但缩放不起作用。我希望有人能给我一些灯光。

我的 .h 文件

@interface TestScrollViewViewController : UIViewController <UIScrollViewDelegate>{

IBOutlet UIScrollView *scrollView;
UIImageView *subview;

}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *subview;

@end

.m 文件

- (void)viewDidLoad {
  [super viewDidLoad];

    UIImage *page1 = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://abc.com/001.jpg"]]];
    UIImage *page2 = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://abc.com/002.jpg"]]];
    UIImage *page3 = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://abc.com/003.jpg"]]];


    NSArray *images = [NSArray arrayWithObjects: page1, page2, page3, nil];
    for (int i = 0; i < images.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = [images objectAtIndex:i];

    self.scrollView.minimumZoomScale=0.5;
    self.scrollView.maximumZoomScale=6.0;
    self.scrollView.delegate=self;

    [self.scrollView addSubview:subview];   

    [subview release];
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);

}

,这是 viewForZoomingInScrollView 方法

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

真的希望有人能提供帮助。谢谢。

劳伦斯

can someone help me on doing zooming with the UIScrollView?

I am trying to add the three images retrieved from an URL and display it using a UIScrollView. I can do the display part but the zooming is not working. I hope someone can show me some lights.

My .h file

@interface TestScrollViewViewController : UIViewController <UIScrollViewDelegate>{

IBOutlet UIScrollView *scrollView;
UIImageView *subview;

}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *subview;

@end

the .m file

- (void)viewDidLoad {
  [super viewDidLoad];

    UIImage *page1 = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://abc.com/001.jpg"]]];
    UIImage *page2 = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://abc.com/002.jpg"]]];
    UIImage *page3 = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://abc.com/003.jpg"]]];


    NSArray *images = [NSArray arrayWithObjects: page1, page2, page3, nil];
    for (int i = 0; i < images.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = [images objectAtIndex:i];

    self.scrollView.minimumZoomScale=0.5;
    self.scrollView.maximumZoomScale=6.0;
    self.scrollView.delegate=self;

    [self.scrollView addSubview:subview];   

    [subview release];
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);

}

and this is the viewForZoomingInScrollView method

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

Really hope someones can help. Thanks.

Lawrence

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

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

发布评论

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

评论(2

爱本泡沫多脆弱 2024-12-05 23:48:53

替换

return self.scrollView;

return subview;

Replace

return self.scrollView;

With

return subview;
柏拉图鍀咏恒 2024-12-05 23:48:53
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.scrollView;
}

这就是问题所在。您不能在此处返回滚动视图本身,您必须返回要缩放的子视图。

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

That's what's wrong. You can't return the scroll view itself here, you must return it's subview that you want to zoom.

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