如何动态调整UIScrollview的大小

发布于 2024-11-06 20:11:09 字数 135 浏览 0 评论 0原文

好吧,我正在使用 UIScrollview 缩放图像。我的 UIScrollview 大小是固定的,所以每当我缩放图像时,它就会被剪切。我想在缩放图像时执行此操作,UIScrollview 也会根据图像增加高度和宽度飞涨.. 任何建议将不胜感激......

well i working with to zoom image with UIScrollview..my UIScrollview size is fix so whenever i make a zoom of a image then it goes cut..i wanna do that as i zoom the image the UIScrollview also increse height and width as per image zoom..
any suggetions will be appreciated......

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

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

发布评论

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

评论(1

三五鸿雁 2024-11-13 20:11:09

这应该可行:-)

  1. 制作一个 UIImage 制作
  2. 一个包含图像的 UIImageView 将
  3. UIImageView 放入 UIScrollView
  4. 将 UIScrollView 的 contentSize 设置为 UIImage 的大小
  5. 记住在 viewForZoomingInScrollView 中返回 UIimageView

将其放入你的 .h 文件:

UIImageView *imageView;

@property (nonatomic, retain) UIImageView *imageView;

并记住合成并释放 imageView

将其放在你想要制作滚动视图的位置:

UIImage *image = [UIImage imageNamed:@"myImage.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:3.0];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
[scrollView setDelegate:self];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
[scrollView release];

添加 UIScrollView 委托方法

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

更新:

在添加上述解决方案时我误解了这个问题。这应该可以做到:-)

kScrollViewAddSize 是一个常量,它定义添加到 UIScrollView 大小的量。

在您的 .h 文件中,您应该添加以下内容:

UIImageView *imageView;
UIScrollView *scrollView;

记住综合并释放它们。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"apple.jpg"];
    imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(kScrollViewAddSize / 2, kScrollViewAddSize / 2, image.size.width, image.size.height)];
    int width = imageView.frame.size.width + kScrollViewAddSize;
    int height = imageView.frame.size.height + kScrollViewAddSize;
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, width, height)];
    [scrollView setMinimumZoomScale:0.5];
    [scrollView setMaximumZoomScale:3.0];
    [scrollView setBackgroundColor:[UIColor redColor]];
    [scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
    [scrollView setDelegate:self];
    [scrollView addSubview:imageView];
    [[self view] addSubview:scrollView];
}

- (void)scrollViewDidZoom:(UIScrollView *)_scrollView {
    int width = imageView.frame.size.width + kScrollViewAddSize;
    int height = imageView.frame.size.height + kScrollViewAddSize;
    [scrollView setFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, imageView.frame.size.width + kScrollViewAddSize, imageView.frame.size.height + kScrollViewAddSize)];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)_scrollView {
    return imageView;
}

viewDidLoad 中添加要创建 UIScrollView 的部分并添加两个委托方法。

This should work :-)

  1. Make a UIImage
  2. Make a UIImageView holding the image
  3. Put the UIImageView into a UIScrollView
  4. Set the contentSize of the UIScrollView to the size of the UIImage
  5. Remember to return the UIimageView in viewForZoomingInScrollView

Put this into your .h file:

UIImageView *imageView;

@property (nonatomic, retain) UIImageView *imageView;

And remember to synthesize and release imageView

Put this where you want to make your scroll view:

UIImage *image = [UIImage imageNamed:@"myImage.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:3.0];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
[scrollView setDelegate:self];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
[scrollView release];

Add the UIScrollView delegate method

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

UPDATE:

I misunderstood the question when adding the above solution. This should do it :-)

kScrollViewAddSize is a constant which defines the amount to add to the UIScrollView's size.

In your .h file you should add the following:

UIImageView *imageView;
UIScrollView *scrollView;

Remember to synthesize and release them.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"apple.jpg"];
    imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(kScrollViewAddSize / 2, kScrollViewAddSize / 2, image.size.width, image.size.height)];
    int width = imageView.frame.size.width + kScrollViewAddSize;
    int height = imageView.frame.size.height + kScrollViewAddSize;
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, width, height)];
    [scrollView setMinimumZoomScale:0.5];
    [scrollView setMaximumZoomScale:3.0];
    [scrollView setBackgroundColor:[UIColor redColor]];
    [scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
    [scrollView setDelegate:self];
    [scrollView addSubview:imageView];
    [[self view] addSubview:scrollView];
}

- (void)scrollViewDidZoom:(UIScrollView *)_scrollView {
    int width = imageView.frame.size.width + kScrollViewAddSize;
    int height = imageView.frame.size.height + kScrollViewAddSize;
    [scrollView setFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, imageView.frame.size.width + kScrollViewAddSize, imageView.frame.size.height + kScrollViewAddSize)];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)_scrollView {
    return imageView;
}

Add the parth in viewDidLoad where you want to create your UIScrollView and add the two delegate methods.

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