iOS tableView实现头部拉伸并改变导航条渐变色

前端之家收集整理的这篇文章主要介绍了iOS tableView实现头部拉伸并改变导航条渐变色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了iOS tableView实现头部拉伸改变,导航条渐变色的具体代码,供大家参考,具体内容如下

  1. #import "TableViewController.h"
  2.  
  3. static NSString *ident = @"cell";
  4.  
  5. #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  6. #define RGB(r,b) RGBA(r,1.0f)
  7. #define ZhuTiColor RGB(76,16,198)
  8. #define ZhuTiColorAlpha(alpha) RGBA(76,198,alpha)
  9.  
  10. // 判断是否是iPhone X
  11. #define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125,2436),[[UIScreen mainScreen] currentMode].size) : NO)
  12. // 状态栏高度
  13. #define STATUS_BAR_HEIGHT (iPhoneX ? 44.f : 20.f)
  14. // 导航栏高度
  15. #define NAVIGATION_BAR_HEIGHT (iPhoneX ? 88.f : 64.f)
  16. // tabBar高度
  17. #define TAB_BAR_HEIGHT (iPhoneX ? (49.f + 34.f) : 49.f)
  18. // home indicator
  19. #define HOME_INDICATOR_HEIGHT (iPhoneX ? 34.f : 0.f)
  20.  
  21. #define ScreenWidth ([UIScreen mainScreen].bounds.size.width)
  22. #define ScreenHeight ([UIScreen mainScreen].bounds.size.height)
  23.  
  24. #define imageHight 200
  25.  
  26. @interface TableViewController ()
  27.  
  28. @property (nonatomic,strong) UIImageView *headImage;
  29. @property (nonatomic,strong) UIView *headerBackView;
  30. @property (nonatomic,strong) UIView *mengView;
  31.  
  32. @end
  33.  
  34. @implementation TableViewController
  35.  
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ident];
  39. self.view.backgroundColor = [UIColor redColor];
  40. self.tableView.tableHeaderView = self.headerBackView;
  41. [self.headerBackView addSubview:self.headImage];
  42. [self.headImage addSubview:self.mengView];
  43. [self navCleanFromAlpha:0];
  44.  
  45. }
  46.  
  47. -(void)navCleanFromAlpha:(CGFloat)alpha
  48. {
  49. [self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:ZhuTiColorAlpha(alpha)] forBarMetrics:UIBarMetricsDefault];
  50. self.navigationController.navigationBar.shadowImage = [UIImage new];
  51. }
  52.  
  53. -(UIImage*) createImageWithColor:(UIColor*) color
  54. {
  55. CGRect rect=CGRectMake(0.0f,0.0f,1.0f,1.0f);
  56. UIGraphicsBeginImageContext(rect.size);
  57. CGContextRef context = UIGraphicsGetCurrentContext();
  58. CGContextSetFillColorWithColor(context,[color CGColor]);
  59. CGContextFillRect(context,rect);
  60. UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  61. UIGraphicsEndImageContext();
  62. return theImage;
  63. }
  64.  
  65. -(UIImageView *)headImage
  66. {
  67. if(!_headImage)
  68. {
  69. _headImage= [[UIImageView alloc]initWithFrame: self.headerBackView.bounds];
  70. _headImage.image = [UIImage imageNamed:@"1024"];
  71. }
  72. return _headImage;
  73. }
  74.  
  75. -(UIView *)mengView
  76. {
  77. if (!_mengView)
  78. {
  79. _mengView = [[UIView alloc]initWithFrame:self.headerBackView.bounds];
  80. _mengView.backgroundColor = RGBA(1,1,0.1);
  81. }
  82. return _mengView;
  83. }
  84.  
  85. -(UIView *)headerBackView
  86. {
  87. if (!_headerBackView)
  88. {
  89. _headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0,ScreenWidth,imageHight)];
  90. [_headerBackView setBackgroundColor:[UIColor lightGrayColor]];
  91. }
  92. return _headerBackView;
  93. }
  94.  
  95. - (void)didReceiveMemoryWarning {
  96. [super didReceiveMemoryWarning];
  97. // Dispose of any resources that can be recreated.
  98. }
  99.  
  100. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  101. {
  102. //---------------------- 图片拉升 -------------------------
  103. //图片高度
  104. CGFloat imageHeight = self.headerBackView.frame.size.height;
  105. //图片宽度
  106. CGFloat imageWidth = ScreenWidth;
  107. //图片上下偏移量
  108. CGFloat imageOffsetY = scrollView.contentOffset.y;
  109.  
  110. // NSLog(@"图片上下偏移量 imageOffsetY:%f ->",imageOffsetY);
  111.  
  112. //上移
  113. if (imageOffsetY < 0)
  114. {
  115. CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
  116. CGFloat f = totalOffset / imageHeight;
  117.  
  118. self.headImage.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5,imageOffsetY,imageWidth * f,totalOffset);
  119. self.mengView.frame = self.headImage.bounds;
  120. }
  121.  
  122. //------------------- 导航条颜色渐变 ----------------------------
  123.  
  124. CGFloat tableViewOffsetY = [self.tableView rectForSection:0].origin.y - NAVIGATION_BAR_HEIGHT;
  125. CGFloat contentOffsetY = scrollView.contentOffset.y;
  126.  
  127. if (contentOffsetY >= tableViewOffsetY)
  128. {
  129. // scrollView.contentOffset = CGPointMake(0,tableViewOffsetY); //定位
  130. [self navCleanFromAlpha:1];
  131.  
  132. }
  133. else
  134. {
  135. CGFloat alpha = scrollView.contentOffset.y/imageHight;
  136.  
  137. if (alpha >= 1) { alpha = 1; }
  138. if (alpha <= 0) { alpha = 0; }
  139. NSLog(@"%.2f",alpha);
  140. [self navCleanFromAlpha:alpha];
  141. }
  142.  
  143. }
  144.  
  145. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  146. return 20;
  147. }
  148.  
  149. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  150. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
  151.  
  152. // Configure the cell...
  153.  
  154. cell.textLabel.text = [NSString stringWithFormat:@"asdada = %zd",indexPath.row];
  155.  
  156. return cell;
  157. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • iOS实现点击图片放大和长按保存图片的示例
  • iOS 点击图片放大效果的实现
  • 解决iOS11图片下拉放大出现信号栏白条的bug问题
  • 利用iOS手势与scrollView代理实现图片的放大缩小
  • iOS tableView实现顶部图片拉伸效果
  • iOS tableview实现顶部拉伸效果
  • iOS TableView头视图根据偏移量下拉缩放效果
  • iOS应用开发中UITableView的分割线的一些设置技巧
  • IOS UITableView和UITableViewCell的几种样式详细介绍
  • iOS tableView实现下拉图片放大效果

猜你在找的iOS相关文章