Nic Hubbard Nic Hubbard - 2 months ago 23
Objective-C Question

Tweetbot banner image pull down to zoom image

Tweetbot and Kickstarter for iOS uses a cool feature on user profiles that have a banner image. If you pull down on the

tableView
the image zooms.

I have it partially working using the following, it changes the height of the image, but strangely, not the width:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

UIImageView *imageView = (UIImageView *)self.tableView.tableHeaderView;
CGFloat y = -scrollView.contentOffset.y;
imageView.frame = CGRectMake(0, scrollView.contentOffset.y, self.cachedImageViewSize.size.width+y, self.cachedImageViewSize.size.height+y);
NSLog(@"%@", NSStringFromCGRect(imageView.frame));

}


Does anyone know how to recreate this effect?

Answer

Ok, I figured it out. Here is what I did:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"church-welcome.png"]];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.cachedImageViewSize = self.imageView.frame;
    [self.tableView addSubview:self.imageView];
    [self.tableView sendSubviewToBack:self.imageView];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 170)];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat y = -scrollView.contentOffset.y;
    if (y > 0) {
        self.imageView.frame = CGRectMake(0, scrollView.contentOffset.y, self.cachedImageViewSize.size.width+y, self.cachedImageViewSize.size.height+y);
        self.imageView.center = CGPointMake(self.view.center.x, self.imageView.center.y);
    }

}
Comments