viewForZoomingInScrollView UIScrollView背景和图像不同步

时间:2011-12-09 11:24:43

标签: iphone ios uiscrollview

我正在放大滚动视图中的图像..但我的滚动视图有一个背景,我希望与前景图像保持同步。

这就是我现在所拥有的。我想知道我是否必须在viewForZoomingInScrollView方法中返回bgImage ..但我只是不确定它是如何或甚至可能活跃。

这就是我在下面要完成的图像缩放...如果有人能帮我弄清楚如何让bg同步滚动,那将非常感激。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.delegate = self;
    scrollView.bounces = NO;
    scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"auckland-300.jpg"]];
    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"249a-134206f1d00-1342071f5d9.ImgPlayerAUCKLAND.png"]];
    scrollView.contentSize = image.frame.size;
    [scrollView addSubview:image];
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 31.0;
    [scrollView setZoomScale:scrollView.minimumZoomScale];
    self.view = scrollView;    
}

#pragma Zooming

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

1 个答案:

答案 0 :(得分:7)

我认为你的意思是你想让背景图像也缩放?我要做的是使用UIImageView作为背景图片,并将该图片和image放入容器视图中,并将其返回viewForZoomingInScrollView:。所以有这样的话:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.delegate = self;
    scrollView.bounces = NO;

    backgroundImage = [[UIImageVIew alloc] initWithImage:[UIImage imageNamed:@"auckland-300.jpg"]];

    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"249a-134206f1d00-1342071f5d9.ImgPlayerAUCKLAND.png"]];

    // Note here you should size the container view appropriately and layout backgroundImage and image accordingly.
    containerView = [[UIView alloc] initWithFrame:image.bounds];
    [containerView addSubview:backgroundImage];
    [containerView addSubview:image];

    scrollView.contentSize = containerView.frame.size;
    [scrollView addSubview:containerView];

    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 31.0;
    [scrollView setZoomScale:scrollView.minimumZoomScale];
    self.view = scrollView;    
}

#pragma Zooming

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