UITableView拉伸效果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITableView拉伸效果相关的知识,希望对你有一定的参考价值。
- 创建一个UITableView 和一个UIImageView
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *headerImgaeView;
- 初始化
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped]; 5 self.tableView.delegate = self; 6 self.tableView.dataSource = self; 7 [self.view addSubview:self.tableView]; 8 9 10 [self layoutHeaderImageView]; 11 }
-
layoutHeaderImageView
1 - (void)layoutHeaderImageView 2 { 3 UIView *headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)]; 4 headerBackView.backgroundColor = [UIColor lightGrayColor]; 5 self.tableView.tableHeaderView = headerBackView; 6 7 self.headerImgaeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)]; 8 self.headerImgaeView.backgroundColor = [UIColor greenColor]; 9 self.headerImgaeView.image = [UIImage imageNamed:@"beautiful.jpg"]; 10 self.headerImgaeView.contentMode = UIViewContentModeScaleAspectFill; 11 self.headerImgaeView.clipsToBounds = YES; 12 [headerBackView addSubview:self.headerImgaeView]; 13 14 }
- 滚动偏移的主要方法(这个为UIScrollViewDelegate中的代理方法)
1 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 2 { 3 CGFloat width = self.view.frame.size.width;// 图片宽度 4 CGFloat yOffset = scrollView.contentOffset.y; //偏移量 5 6 NSLog(@": %.2f", yOffset); 7 8 if (yOffset < 0) { 9 CGFloat totalOffset = 150 + ABS(yOffset); 10 CGFloat f = totalOffset / 150; //缩放系数 11 12 self.headerImgaeView.frame = CGRectMake(-(width * f - width) / 2, yOffset, width * f, totalOffset); //拉伸后的frame是同比例缩放 13 } 14 15 if (yOffset > 0) { 16 CGFloat totalOffset = 150 - ABS(yOffset); 17 CGFloat f = totalOffset / 150; 18 19 self.headerImgaeView.frame = CGRectMake(-(width * f - width) / 2, yOffset, width * f, totalOffset); 20 } 21 }
以上是关于UITableView拉伸效果的主要内容,如果未能解决你的问题,请参考以下文章