Ios 在UITableView中缩放滚动图像

Ios 在UITableView中缩放滚动图像,ios,objective-c,uitableview,uiimageview,xcode6,Ios,Objective C,Uitableview,Uiimageview,Xcode6,我试图重现这种效果- UITableView的标题部分中有一个UIImageView。当用户向下滚动时,图像的大小减小并在导航栏下滚动,当用户向上滚动时,UIImageView将展开并返回其原始位置 Twitter for iOS对您的个人资料有此影响 到目前为止,这是我所知道的-代码将在scrollViewDidScroll方法下,我将不得不使用两个图像,一个大一个小。我需要了解如何在方法内部的这两个图像之间转换的帮助。如果您想实现Twitter滚动扩展UIImageView,Yari D'a

我试图重现这种效果-

UITableView的标题部分中有一个UIImageView。当用户向下滚动时,图像的大小减小并在导航栏下滚动,当用户向上滚动时,UIImageView将展开并返回其原始位置

Twitter for iOS对您的个人资料有此影响


到目前为止,这是我所知道的-代码将在scrollViewDidScroll方法下,我将不得不使用两个图像,一个大一个小。我需要了解如何在方法内部的这两个图像之间转换的帮助。

如果您想实现Twitter滚动扩展UIImageView,Yari D'areglia提供了一个教程,概述了如何实现这一点。你可能会发现它很有用。该链接包含一段视频,用于演示所需效果

  • 摘自迈克尔·亨利:

我将图像放在tableview的后面,并在滚动时调整其大小
contentInset
contentOffset
都是最完美的工具,可以使此效果以最少的努力工作。
#define IMAGE_VIEW_TAG 100
#define IMAGE_SCROLL_VIEW_TAG 101

    #pragma mark - ScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        if(scrollView.tag == IMAGE_SCROLL_VIEW_TAG) return;

    UITableView * tv = (UITableView*) scrollView;
    UITableViewCell * cell = [tv cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    UIScrollView * svImage = (UIScrollView*)[cell viewWithTag:IMAGE_SCROLL_VIEW_TAG];
    CGRect frame = svImage.frame;
    frame.size.height =  MAX((_imageHeaderHeight-tv.contentOffset.y),0);
    frame.origin.y = tv.contentOffset.y;
    svImage.frame = frame;
    svImage.zoomScale = 1 + (abs(MIN(tv.contentOffset.y,0))/320.0f);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
        return [scrollView viewWithTag:IMAGE_VIEW_TAG];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell * cell;

        NSLog(@"Row %@", indexPath);
        if(indexPath.row == 0){
                static NSString * headerId = @"headerCell";
                cell = [tableView dequeueReusableCellWithIdentifier:headerId];
                if(!cell) {

// create cell as it doesn't exist
                            cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, _imageHeaderHeight)];
                            cell.backgroundColor = [UIColor clearColor];
                            cell.selectionStyle = UITableViewCellSelectionStyleNone;

                        // image view
                        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, cell.contentView.frame.size.width, _imageHeaderHeight)];
                        imageView.contentMode = UIViewContentModeScaleAspectFill;
                        imageView.tag = IMAGE_VIEW_TAG;
                        imageView.clipsToBounds = YES;
                        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

                        UIScrollView * svImage = [[UIScrollView alloc]initWithFrame:imageView.frame];
                        svImage.delegate = self;
                        svImage.userInteractionEnabled = NO;

                        [svImage addSubview:imageView];

                        // image
                        svImage.tag = IMAGE_SCROLL_VIEW_TAG;
                        svImage.backgroundColor = [UIColor blackColor];
                        svImage.zoomScale = 1.0f;
                        svImage.minimumZoomScale = 1.0f;
                        svImage.maximumZoomScale = 2.0f;
                        [cell.contentView addSubview:svImage];
                        UIImageView * headerInfo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];

                    [cell.contentView addSubview:headerInfo];

                    CGRect headerFrame = headerInfo.frame;
                    headerFrame.size.height = 149.0f;
                    headerFrame.origin.y = _imageHeaderHeight - 149.0f;
                    headerInfo.frame = headerFrame;
}
// cell exists - grab a reference to the image it displays
            UIImageView *imageView = (UIImageView*)[cell viewWithTag:IMAGE_VIEW_TAG];

// maybe change the imageView
return cell;
}