iOS Developer

tableView向下拉拽, 拉伸图片, 向上活动渐变导航栏

2017-05-14  本文已影响67人  91阿生

先上效果图, 说明下:

这篇文章说的知识点是向下拉伸图片, 与向上滑动到一定程度后显示导航栏.
gif-1.gif
  #define RGB(r, g, b) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:1]
 #define kRandomColor  RGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

static CGFloat const kHeaderIconHeight = 200;
static CGFloat const kNavHeight = 64;
static NSString *const kCellidentifire = @"UITableViewCell";

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
// UI部分
/**头部图片视图*/
@property (nonatomic, strong) UIImageView *icon;
/**tableview*/
@property (nonatomic, strong) UITableView *tableView;
// 数据部分
/**记录最初的OffsetY-->-200. 用于与当前滑动的offsetY做差值比较*/
@property (nonatomic, assign) CGFloat oriOffsetY;
@end

@implementation ViewController

- (void)viewDidLoad {
     [super viewDidLoad];
     // -1.当控制器的第一个控件为继承自scrollview的控件时, 系统会帮我们自动设置64. 这里我不许要, 因此设置no
     self.automaticallyAdjustsScrollViewInsets = NO;
     // 0.先设置导航栏.
     [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
     [self.navigationController.navigationBar setShadowImage:[UIImage new]];
         // 1.添加tableView
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
     self.tableView.delegate = self;
     self.tableView.dataSource = self;
     [self.view addSubview:self.tableView];
     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellidentifire];
     // 2.添加头部图片(固定高度200)
     self.icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pbg"]];
     self.icon.frame = CGRectMake(0, 0, self.view.bounds.size.width, kHeaderIconHeight);
     [self.view addSubview:self.icon];
      // 3.初始化设置tableView的contentInset
      self.oriOffsetY = -kHeaderIconHeight;
      self.tableView.contentInset = UIEdgeInsetsMake(kHeaderIconHeight, 0, 0, 0);
 }

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellidentifire];

    cell.textLabel.text = [NSString stringWithFormat:@"%zd", indexPath.item];
    cell.backgroundColor = kRandomColor;

    return cell;
}
上一篇下一篇

猜你喜欢

热点阅读