欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 13:25:28
本文实例为大家分享了ios tableview实现头部拉伸改变,导航条渐变色的具体代码,供大家参考,具体内容如下 #import "tableviewcontro...

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

#import "tableviewcontroller.h"

static nsstring *ident = @"cell";

#define rgba(r,g,b,a) [uicolor colorwithred:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define rgb(r,g,b) rgba(r,g,b,1.0f)
#define zhuticolor rgb(76,16,198)
#define zhuticoloralpha(alpha) rgba(76, 16, 198, alpha)

// 判断是否是iphone x
#define iphonex ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(1125, 2436), [[uiscreen mainscreen] currentmode].size) : no)
// 状态栏高度
#define status_bar_height (iphonex ? 44.f : 20.f)
// 导航栏高度
#define navigation_bar_height (iphonex ? 88.f : 64.f)
// tabbar高度
#define tab_bar_height (iphonex ? (49.f + 34.f) : 49.f)
// home indicator
#define home_indicator_height (iphonex ? 34.f : 0.f)

#define screenwidth ([uiscreen mainscreen].bounds.size.width)
#define screenheight ([uiscreen mainscreen].bounds.size.height)

#define imagehight 200


@interface tableviewcontroller ()

@property (nonatomic,strong) uiimageview *headimage;
@property (nonatomic, strong) uiview *headerbackview;
@property (nonatomic, strong) uiview *mengview;

@end

@implementation tableviewcontroller

- (void)viewdidload {
  [super viewdidload];
  [self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:ident];
  self.view.backgroundcolor = [uicolor redcolor];
  self.tableview.tableheaderview = self.headerbackview;
  [self.headerbackview addsubview:self.headimage];
  [self.headimage addsubview:self.mengview];
  [self navcleanfromalpha:0];
  
}

-(void)navcleanfromalpha:(cgfloat)alpha
{
  [self.navigationcontroller.navigationbar setbackgroundimage:[self createimagewithcolor:zhuticoloralpha(alpha)] forbarmetrics:uibarmetricsdefault];
  self.navigationcontroller.navigationbar.shadowimage = [uiimage new];
}

-(uiimage*) createimagewithcolor:(uicolor*) color
{
  cgrect rect=cgrectmake(0.0f, 0.0f, 1.0f, 1.0f);
  uigraphicsbeginimagecontext(rect.size);
  cgcontextref context = uigraphicsgetcurrentcontext();
  cgcontextsetfillcolorwithcolor(context, [color cgcolor]);
  cgcontextfillrect(context, rect);
  uiimage *theimage = uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
  return theimage;
}

-(uiimageview *)headimage
{
  if(!_headimage)
  {
    _headimage= [[uiimageview alloc]initwithframe: self.headerbackview.bounds];
    _headimage.image = [uiimage imagenamed:@"1024"];
  }
  return _headimage;
}

-(uiview *)mengview
{
  if (!_mengview)
  {
    _mengview = [[uiview alloc]initwithframe:self.headerbackview.bounds];
    _mengview.backgroundcolor = rgba(1, 1, 1, 0.1);
  }
  return _mengview;
}

-(uiview *)headerbackview
{
  if (!_headerbackview)
  {
    _headerbackview = [[uiview alloc] initwithframe:cgrectmake(0, 0, screenwidth, imagehight)];
    [_headerbackview setbackgroundcolor:[uicolor lightgraycolor]];
  }
  return _headerbackview;
}

- (void)didreceivememorywarning {
  [super didreceivememorywarning];
  // dispose of any resources that can be recreated.
}

- (void)scrollviewdidscroll:(uiscrollview *)scrollview
{
  //---------------------- 图片拉升 -------------------------
  //图片高度
  cgfloat imageheight = self.headerbackview.frame.size.height;
  //图片宽度
  cgfloat imagewidth = screenwidth;
  //图片上下偏移量
  cgfloat imageoffsety = scrollview.contentoffset.y;
  
//  nslog(@"图片上下偏移量 imageoffsety:%f ->",imageoffsety);
  
  //上移
  if (imageoffsety < 0)
  {
    cgfloat totaloffset = imageheight + abs(imageoffsety);
    cgfloat f = totaloffset / imageheight;
    
    self.headimage.frame = cgrectmake(-(imagewidth * f - imagewidth) * 0.5, imageoffsety, imagewidth * f, totaloffset);
    self.mengview.frame = self.headimage.bounds;
  }
  
  //------------------- 导航条颜色渐变 ----------------------------
  
  cgfloat tableviewoffsety = [self.tableview rectforsection:0].origin.y - navigation_bar_height;
  cgfloat contentoffsety = scrollview.contentoffset.y;

  if (contentoffsety >= tableviewoffsety)
  {
//    scrollview.contentoffset = cgpointmake(0, tableviewoffsety); //定位
    [self navcleanfromalpha:1];

  }
  else
  {
    cgfloat alpha = scrollview.contentoffset.y/imagehight;
    
    if (alpha >= 1) { alpha = 1; }
    if (alpha <= 0) { alpha = 0; }
    nslog(@"%.2f",alpha);
    [self navcleanfromalpha:alpha];
  }
  
  
}

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
  return 20;
}


- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
  uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:ident forindexpath:indexpath];
  
  // configure the cell...
  
  cell.textlabel.text = [nsstring stringwithformat:@"asdada = %zd",indexpath.row];
  
  return cell;
}

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

上一篇:

下一篇: