1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| #import "YHBaseHeaderAnimatedViewController.h"
@interface YHBaseHeaderAnimatedViewController () { //承载视图 UIView * _privteBGHeaderView; //宽高比例 CGFloat _privteRate; //原始宽高 CGFloat _privteOriWidth; CGFloat _privteOriHeught; UIVisualEffectView * _blurView; UINavigationBar * _naviBar; } @end
@implementation YHBaseHeaderAnimatedViewController
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.edgesForExtendedLayout = UIRectEdgeNone; _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; [self.view addSubview:_tableView]; _tableView.delegate=self; _tableView.dataSource=self; [self YHCreatView]; // Do any additional setup after loading the view. }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //对导航进行处理 -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if (self.navigationController) { [self.navigationController setNavigationBarHidden:YES animated:YES]; [self showNavigationBar]; } } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; if (self.navigationController) { [self.navigationController setNavigationBarHidden:NO animated:YES]; } } -(void)showNavigationBar{ if (_naviBar==nil) { _naviBar = [[UINavigationBar alloc]init]; [self.view addSubview:_naviBar]; _naviBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 64); [_naviBar setBackgroundImage:[UIImage imageNamed:@"clear"] forBarMetrics:UIBarMetricsDefault]; [_naviBar setShadowImage:[UIImage imageNamed:@"clear"]]; _naviBar.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0]; UINavigationItem * item = [[UINavigationItem alloc]initWithTitle:self.title]; _naviBar.items = @[item]; item.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(popToLast)]; } if (self.animatedHeaderView==nil) { _naviBar.hidden=YES; }else{ _naviBar.hidden=NO; } } -(void)popToLast{ [self.navigationController popViewControllerAnimated:YES]; } -(void)YHCreatView{ if (self.maxScrollOffset==0) { self.maxScrollOffset=40; } if (self.minAlpha==0) { self.minAlpha = 0.5; } if (self.maxBluer==0) { self.maxBluer = 1; } _privteBGHeaderView = [[UIView alloc]init]; _privteBGHeaderView.backgroundColor = [UIColor clearColor]; self.tableView.backgroundColor = [UIColor clearColor]; _blurView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]]; [self reloadAnimatedView]; }
-(void)reloadAnimatedView{ CGFloat viewH=0; CGFloat aniH=0; for (UIView * view in _privteBGHeaderView.subviews) { [view removeFromSuperview]; } if (self.tableHeaderView) { viewH = viewH+self.tableHeaderView.bounds.size.height; [_privteBGHeaderView addSubview:self.tableHeaderView]; } if (self.animatedHeaderView) { _privteRate = self.animatedHeaderView.frame.size.width/self.animatedHeaderView.frame.size.height; [self.animatedHeaderView addSubview:_blurView]; _blurView.frame= self.animatedHeaderView.bounds; _blurView.alpha=0; _privteOriWidth = self.animatedHeaderView.frame.size.width; _privteOriHeught = self.animatedHeaderView.frame.size.height; if (self.headerHeight!=0) { viewH+=self.headerHeight; aniH = self.headerHeight; }else{ viewH+=self.view.frame.size.width/self.animatedHeaderView.bounds.size.width*self.animatedHeaderView.frame.size.height; aniH = self.animatedHeaderView.bounds.size.height; } [self.view insertSubview:self.animatedHeaderView atIndex:0]; } self.tableHeaderView.frame=CGRectMake(0, aniH, self.tableHeaderView.frame.size.width, self.tableHeaderView.frame.size.height); _privteBGHeaderView.frame=CGRectMake(0, 0, self.view.frame.size.width, viewH); self.tableView.tableHeaderView = _privteBGHeaderView; } -(void)setAnimatedHeaderView:(UIView *)animatedHeaderView{ if (animatedHeaderView==nil) { if (_animatedHeaderView) { [_animatedHeaderView removeFromSuperview]; } } _animatedHeaderView = animatedHeaderView; } -(void)setMinAlpha:(CGFloat)minAlpha{ if (minAlpha<=0) { minAlpha=0.001; } if (minAlpha>=1) { minAlpha=0.999; } _minAlpha=minAlpha; } -(void)setMaxBluer:(CGFloat)maxBluer{ if (maxBluer<=0) { maxBluer = 0.001; } if (maxBluer>=1) { maxBluer = 0.999; } _maxBluer = maxBluer; } -(void)setNaviColor:(UIColor *)naviColor{ _naviColor = naviColor; _naviBar.backgroundColor = naviColor; } -(void)setLeftBarButtons:(NSArray *)leftBarButtons{ _naviBar.topItem.leftBarButtonItems = leftBarButtons; _leftBarButtons = leftBarButtons; } -(void)setRightBarButtons:(NSArray *)rightBarButtons{ _naviBar.topItem.rightBarButtonItems = rightBarButtons; _rightBarButtons = rightBarButtons; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (self.animatedHeaderView==nil) { return; } if (self.tableView==scrollView) { //获取到偏移量 默认两倍 CGFloat offset = scrollView.contentOffset.y*2; //如果小于0 则进行形变拉伸操作 if (offset<=0) { if (offset<-self.maxScrollOffset*2) { }else{ CGFloat height = _privteOriHeught+abs((int)offset); CGFloat width = height*_privteRate; self.animatedHeaderView.frame = CGRectMake(self.view.frame.size.width/2-width/2, offset/2, width, height); } if (_naviBar) { if (_naviBar.shadowImage==nil) { [_naviBar setShadowImage:[UIImage imageNamed:@"clear"]]; } } }else{ //如果大于零 进行推出操作 if (self.animatedlevel==YHBaseHeaderAnimatedLevelSlow) { //慢速 self.animatedHeaderView.frame = CGRectMake(0, -offset/8, self.animatedHeaderView.frame.size.width, self.animatedHeaderView.frame.size.height); }else if(self.animatedlevel==YHBaseHeaderAnimatedLevelFast){ //快速 self.animatedHeaderView.frame = CGRectMake(0, -offset/2, self.animatedHeaderView.frame.size.width, self.animatedHeaderView.frame.size.height); }else{ //正常 self.animatedHeaderView.frame = CGRectMake(0, -offset/4, self.animatedHeaderView.frame.size.width, self.animatedHeaderView.frame.size.height); } //进行渐隐动画处理 CGFloat tmp = offset/2/_privteOriHeught; if (self.alphaAnimated) { self.animatedHeaderView.alpha = 1-(1-self.minAlpha)*tmp; } //进行模糊动画 if (self.bluerAnimated) { _blurView.alpha = self.maxBluer*tmp; } //进行导航显示 if (_naviBar) { _naviBar.backgroundColor = [_naviBar.backgroundColor colorWithAlphaComponent:tmp]; if (_naviBar.shadowImage) { [_naviBar setShadowImage:nil]; } } } } } @end
|