iOS实现头部拉伸效果

所属分类: 软件编程 / IOS 阅读数: 1634
收藏 0 赞 0 分享

本文实例为大家分享了iOS实现头部拉伸效果展示的具体代码,供大家参考,具体内容如下

主要涉及到导航栏透明度、图片拉伸、列表头部等。

  • 导航栏透明度的实现。
  • 列表拖动距离的监听,及图片放大的实现。

导航透明度的设置

添加系统导航栏的Category实现

声明部分:

@interface UINavigationBar (BackgroundColor)
- (void)lt_setBackgroundColor:(UIColor *)color;
@end

实现部分:

#import <objc/runtime.h>

@implementation UINavigationBar (BackgroundColor)
static char overlayKey;

- (UIView *)overlay
{
  return objc_getAssociatedObject(self, &overlayKey);
}

- (void)setOverlay:(UIView *)overlay
{
  objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)lt_setBackgroundColor:(UIColor *)color
{
  if (!self.overlay) {
    [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    // insert an overlay into the view hierarchy
    self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, self.bounds.size.height + 20)];
    self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self insertSubview:self.overlay atIndex:0];
  }
  self.overlay.backgroundColor = color;
}

@end

监听列表拖动及实现图片放大

主要是监听滚动的距离(scrollViewDidScroll:方法)

#import "StretchViewController.h"
#import "UINavigationBar+BackgroundColor.h"

// 背景图片的宽高比例
#define ratio 0.8

@interface StretchViewController () <UITableViewDelegate, UITableViewDataSource>

// 可放大的背景图片
@property (nonatomic, strong) UIImageView *bgView;
// 记录原始大小
@property (assign) CGRect originalFrame;

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation StretchViewController

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  //[self.navigationController setNavigationBarHidden:YES animated:animated];

  //self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  //self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
  //self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
  // 设置导航栏底部分割线为透明
  [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // 设置全透明
  [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0]];

  // Do any additional setup after loading the view.
  self.view.backgroundColor = [UIColor lightGrayColor];

  self.bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width*ratio)];
  self.bgView.image = [UIImage imageNamed:@"bg-mine"];
  self.originalFrame = self.bgView.frame;
  [self.view addSubview:self.bgView];

  self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
  self.tableView.backgroundColor = [UIColor clearColor];
  self.tableView.showsVerticalScrollIndicator = NO;
  self.tableView.delegate = self;
  self.tableView.dataSource = self;

  // 1. contentInset
  //table.contentInset = UIEdgeInsetsMake(160, 0, 0, 0);

  // 2. heatView
  UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 160)];
  headView.backgroundColor = [UIColor clearColor];
  self.tableView.tableHeaderView = headView;
  [self.view addSubview:self.tableView];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"];
  }
  cell.textLabel.text = @"测试数据";
  return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 10;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

  CGFloat yOffset = scrollView.contentOffset.y; // 向上滑动,offset是增加的;向下滑动,是减少的
  if (yOffset < 160) { // 当滑动到导航栏底部时
    CGFloat colorAlpha = yOffset/160;

//    self.navigationController.navigationBar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:colorAlpha];
    [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:colorAlpha]];

  } else { // 超过导航栏底部了
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
  }

  // 往上滑动效果、处理放大效果
  if (yOffset > 0) {
    self.bgView.frame = ({
      CGRect frame = self.bgView.frame;
      frame.origin.y = self.originalFrame.origin.y - yOffset;
      frame;
    });
  } else { // 往下移动,放大效果
    self.bgView.frame = ({
      CGRect frame = self.originalFrame;
      frame.size.height = self.originalFrame.size.height - yOffset;
      frame.size.width = frame.size.height/ratio;

      //
      frame.origin.x = self.originalFrame.origin.x - (frame.size.width - self.originalFrame.size.width)/2;
      frame;
    });
  }

}
@end

以上是对系统原生的导航栏进行透明度设置。

也可进行自定义视图设置为导航栏

效果如下:

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

更多精彩内容其他人还在看

详解IOS中如何实现瀑布流效果

说到瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹果API新加进的collectionView进行的瀑布流封装! 确实,不论是写起来还是用起来都要方便很多!那么下面一起来看看I
收藏 0 赞 0 分享

iOS用两行代码完美解决数据持久化

所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据。在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一种巧妙的方法,用两行代码解决这个问题,一起来学习下。
收藏 0 赞 0 分享

IOS游戏开发之五子棋OC版

五子棋是大家比较熟悉的一款小游戏,相信很多人用多种语言写过五子棋小游戏,本文试着用OC实现了一下,在这里给大家分享一下。有需要的可以参考学习。
收藏 0 赞 0 分享

扫描二维码控件的封装iOS实现

这篇文章主要为大家详细介绍了iOS实现扫描二维码控件的封装,具有一定的实用性和参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现侧滑栏效果

这篇文章主要为大家详细介绍了iOS实现侧滑栏效果,点击侧边拉出相应菜单,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现水平方向瀑布流

这篇文章主要为大家详细介绍了iOS实现水平方向瀑布流的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 汉字的拼音

本文通过一段代码给代码介绍了ios汉字的拼音知识及将汉字转拼音的代码,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
收藏 0 赞 0 分享

如何实现IOS_SearchBar搜索栏及关键字高亮

本文通过实例代码演示如何实现IOS搜索栏及搜索关键字高亮,效果很棒,小编觉得对大家的学习会很有帮助,现在分享给大家,有需要的可以参考学习。
收藏 0 赞 0 分享

浅析iOS多视图滑动点击切换的集成

本文将大家常常会用到的多视图滑动点击切换视图进行封装,这样在大家使用的时候就很方便了,有需要的可以参考学习,下面一起来看看吧。
收藏 0 赞 0 分享

用iOS代码获取APP启动页图片

这篇文章主要为大家详细介绍了用iOS代码获取APP启动页图片的相关资料,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多