简体   繁体   English

表格视图控制器中的图像在滚动表格视图时放大

[英]Image in table view controller zoom in when scroll table view

I want to make a Table View Controller that hold a table view and an image view in table view header view. 我想制作一个在表视图标题视图中包含一个表视图和一个图像视图的表视图控制器。 When I scroll down the table view I want to zoom in the image (like in CNN app for iOS). 当我向下滚动表格视图时,我想放大图像(例如在iOS的CNN应用中)。 I tried a lot of things but none of them to this as I want. 我尝试了很多东西,但我没有想要这样做。 Maybe someone can help me. 也许有人可以帮助我。 Thanks! 谢谢!

All screen will look like this image like in this image: 所有屏幕都将如下图所示:


(source: mzstatic.com ) (来源: mzstatic.com

I tried to do this by making the image at the top the header view of the table, but couldn't get that to work. 我试图通过使图像位于表格的顶部视图的顶部来实现此目的,但无法使其正常工作。

The way that did work, was to add a scroll view to the top of a UIViewController's view with a image view inside it. 起作用的方法是将滚动视图添加到UIViewController的视图顶部,并在其中包含图像视图。 I then added a table view, and made it the same size as the controller's view so that it is over top the small scroll view that holds the image (this was done in a storyboard with auto layout turned off). 然后,我添加了一个表格视图,并使其大小与控制器的视图大小相同,以使其位于保存该图像的小滚动视图之上(此操作在情节提要中关闭了自动布局)。 In code, I add a table header view that's transparent and has a label that acts as the caption for the picture. 在代码中,我添加了一个透明的表头视图,并具有充当图片标题的标签。 It has the same height as the small scroll view. 它具有与小滚动视图相同的高度。 When you scroll the content of the table view up, I also scroll the small scroll view but at half the rate. 当您向上滚动表格视图的内容时,我也会以较小的速率滚动小滚动视图。 If you pull the content down, I change the frame of the small scroll view so that it expands and stays centered. 如果您将内容下拉,则将更改小滚动视图的框架,以使其扩展并保持居中。 Here's the code: 这是代码:

@interface ViewController ()
@property (strong,nonatomic) NSArray *theData;
@property (weak,nonatomic) IBOutlet UITableView *tableView;
@property (weak,nonatomic) IBOutlet UIScrollView *imageScrollView;
@property (nonatomic) CGRect svFrame;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.imageScrollView.delegate = self;
    self.imageScrollView.contentSize = CGSizeMake(500, 500);
    self.svFrame = self.imageScrollView.frame;
    self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine"];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 135)];
    headerView.backgroundColor = [UIColor clearColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, headerView.frame.size.width, 24)];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = @"This is a Picture Caption";
    [headerView addSubview:label];
    self.tableView.tableHeaderView = headerView;

    [self.tableView reloadData];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    cell.backgroundView = bgView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.theData.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.theData[indexPath.row];
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (! [scrollView isEqual:self.imageScrollView]) {
        if (scrollView.contentOffset.y > 0) {
            self.imageScrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y/2);
        }else{
            self.imageScrollView.frame = CGRectMake(scrollView.contentOffset.y, scrollView.contentOffset.y, self.svFrame.size.width - (scrollView.contentOffset.y * 2), self.svFrame.size.height - (scrollView.contentOffset.y * 2));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM