viewForZoomingInScrollView

时间:2013-02-03 13:59:44

标签: iphone xcode

我有几个按钮,每个按钮都加载一个不同的图像UIScrollView,你可以放大这些图像。发生的事情是,在选择新图像后,前一个图像仍然存在于滚动视图中。如何在打开新图像之前清理滚动视图?

这是到目前为止的代码:

- (UIView *) viewForZoomingInScrollView: (UIScrollView *)scrollView{
    return imgView;
}
-(IBAction)buttonClicked: (UIButton *)sender{
    switch (sender.tag) {
        case 1:{
            UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Gath.jpg"]];
            self.imgView = myImageView;
            [myImageView release];

            scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
            scrollView.maximumZoomScale = 4.0;
            scrollView.minimumZoomScale = 1.0;
            scrollView.clipsToBounds = YES;
            scrollView.delegate = self;
            [scrollView addSubview:imgView];
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您不断添加新的子视图而不删除之前的子视图。您只需替换现有imageView中的图像内容,而不是创建新图像内容。

替换它:

   UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Gath.jpg"]];
    self.imgView = myImageView;
    [myImageView release];

    self.imageView.image = [UIImage imageNamed:@"Gath.jpg"];

并删除此

    [scrollView addSubview:imgView];

我假设您已将imageView链接到其他地方的self.imageView,例如在故事板中,或者在创建scrollView时。

答案 1 :(得分:0)

你可以试试这个,

[myImageView removeFromSuperView];

myImageView =无;

myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@“Gath.jpg”]];