如何动态调整UIScrollview的大小

时间:2011-05-14 08:21:03

标签: iphone objective-c uiscrollview image-zoom

我正在使用UIScrollview来缩放图像..我的UIScrollview大小是固定的,所以每当我对图像进行缩放然后它就会被剪切..我想这样做,因为我缩放图像UIScrollview也增加高度和宽度根据图像缩放.. 任何建议将不胜感激......

1 个答案:

答案 0 :(得分:3)

这应该有效: - )

  1. 制作UIImage
  2. 制作一个UIImageView,保存图像
  3. 将UIImageView放入UIScrollView
  4. 将UIScrollView的contentSize设置为UIImage的大小
  5. 请记住在viewForZoomingInScrollView
  6. 中返回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的parth并添加两个委托方法。