自定义TableView的顶部HeaderView,在Headerview里面添加ScrollView,实现左右滑动并且下拉等比例方法
.h
@interface HeaderImageView : UIView
//改变图片的大小
- (void)setOffset:(CGFloat)offsetY;
@end
.m
#import "HeaderImageView.h"
#import "UIView+DSLExtension.h"
@interface HeaderImageView()
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageController;
@property(nonatomic,strong)UIView *agencyBgView;//机构父View
@property(nonatomic,strong)UIImageView *agencyIconImageView;//机构头像
@property(nonatomic,strong)UILabel *workMainNameLabel;//机构名称
@property(nonatomic,strong)UILabel *certificationLabel;//机构认证
@end
@implementation HeaderImageView
{
CGFloat page;
}
#pragma mark- 懒加载
- (UIView *)agencyBgView{
if (!_agencyBgView) {
_agencyBgView = [[UIView alloc]init];
[self addSubview:_agencyBgView];
}
return _agencyBgView;
}
- (UIImageView *)agencyIconImageView{
if (!_agencyIconImageView) {
_agencyIconImageView = [[UIImageView alloc]init];
[self.agencyBgView addSubview:_agencyIconImageView];
}
return _agencyIconImageView;
}
- (UILabel *)workMainNameLabel{
if (!_workMainNameLabel) {
_workMainNameLabel = [[UILabel alloc]init];
[self.agencyBgView addSubview:_workMainNameLabel];
}
return _workMainNameLabel;
}
- (UILabel *)certificationLabel{
if (!_certificationLabel) {
_certificationLabel = [[UILabel alloc]init];
[self.agencyBgView addSubview:_certificationLabel];
_certificationLabel.textColor = [UIColor lightGrayColor];
_certificationLabel.font = [UIFont systemFontOfSize:13];
}
return _certificationLabel;
}
#pragma mark- 初始化
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
//创建背景View
/*必须添加bgView*/
UIView *bgView = [[UIView alloc]initWithFrame:frame];
bgView.dsl_height = Screen_Width * (9.0/16);
[self addSubview:bgView];
self.bgView = bgView;
//创建ScrollView
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:frame];
scrollView.dsl_height = Screen_Width * (9.0/16);
[self.bgView addSubview:scrollView];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.clipsToBounds = NO;
scrollView.bounces = NO;
self.scrollView = scrollView;
//创建轮播图片
NSArray *imageArrays = @[@"http://i-imgp.fetionpic.com/fpic/pic1/M00/03/36/rBUyIVHbzL_UQHqzAAN-5r0lZ3c175.jpg",
@"http://imgsrc.baidu.com/forum/pic/item/a9773912b31bb0513c8dc1a2367adab44bede08b.jpg",
@"http://g.hiphotos.baidu.com/zhidao/pic/item/2f738bd4b31c870177a1ba3d277f9e2f0608ffd2.jpg"];
[imageArrays enumerateObjectsUsingBlock:^(NSString *imageStr, NSUInteger index, BOOL * _Nonnull stop) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * index, 0, [UIScreen mainScreen].bounds.size.width, scrollView.dsl_height)];
[scrollView addSubview:imageView];
[imageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.tag = index + 20;
imageView.clipsToBounds = YES;
}];
scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * imageArrays.count, 0);
//创建pageController
UIPageControl *pageController = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height - 15 - 45 - Margin_Width * 2, 0, 0)];
[self.bgView addSubview:pageController];
pageController.numberOfPages = imageArrays.count;
pageController.currentPageIndicatorTintColor = [UIColor redColor];//当前
pageController.pageIndicatorTintColor = [UIColor whiteColor];//非当前
self.pageController = pageController;
self.agencyBgView.backgroundColor = [UIColor whiteColor];
[self.agencyIconImageView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493895745509&di=8a4e0cc81fed5017d5a2ee1eb878447b&imgtype=0&src=http%3A%2F%2Fwww.lagou.com%2Fi%2Fimage%2FM00%2F01%2F1B%2FCgqKkVZf9BaASo6tAACEgPXklnA230.jpg"] placeholderImage:nil];
self.workMainNameLabel.text = @"XXXXXXX";
//self.workMainNameLabel.backgroundColor = DSLRandomColor;
self.certificationLabel.text = @"XXXX:XXXXXXXXXX";
//certificationLabel.backgroundColor = DSLRandomColor;
[self.agencyBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@(Screen_Width * (9.0/16) + Margin_Width));
make.width.equalTo(@(Screen_Width));
make.height.equalTo(@(45));
make.left.equalTo(self);
}];
[self.agencyIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.agencyBgView);
//make.top.equalTo(cycleScrollView.mas_bottom).offset(Margin_Height);
make.width.height.equalTo(@(45));
make.left.equalTo(self.agencyBgView).offset(15);
}];
[self.workMainNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.agencyIconImageView);
make.width.equalTo(@(200));
make.left.equalTo(self.agencyIconImageView.mas_right).offset(Margin_Width);
make.height.equalTo(@(20));
}];
[self.certificationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(20));
make.left.equalTo(self.workMainNameLabel);
make.bottom.equalTo(self.agencyIconImageView);
make.right.equalTo(self).offset(-Margin_Width);
}];
}
return self;
}
- (void)setOffset:(CGFloat)offsetY{
offsetY = offsetY < 0 ? offsetY : offsetY * 0.8;
//背景View
self.bgView.frame = CGRectMake(0, offsetY,Screen_Width,Screen_Width *(9.0/16) );
//变大的图片
UIImageView *imageView = [self.scrollView viewWithTag:page + 20];
imageView.frame = CGRectMake(Screen_Width * page, 0, Screen_Width, Screen_Width *(9.0/16) - offsetY);
//self.pageController
self.pageController.frame = CGRectMake(self.frame.size.width/2, self.frame.size.height - 15 - 45 - Margin_Width * 2 - offsetY, 0, 0);
}
#pragma mark- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
page = self.scrollView.contentOffset.x / Screen_Width;
self.pageController.currentPage = page;
}
@end
调用
headerView = [[HeaderImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width * (9.0/16) + 45 + Margin_Width * 2)];
self.tableView.tableHeaderView = headerView;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[headerView setOffset:self.tableView.contentOffset.y];
}
下载源码