下拉放大图片和毛玻璃的效果

思路: 我们通过监听 tableView 的下拉偏移量,通过偏移量的大小相应的改变顶部图片的 frame 而由于照片的填充模式,就会产生一个放大的效果!同时根据偏移量我们去修改毛玻璃的透明度就会营造出由模糊到清晰的视觉效果!

代码如下:

@property (nonatomic ,strong)UITableView * tableView;//下面的tableView

@property (nonatomic,strong)UIImageView * topImageView;//顶部的imageView

@property (nonatomic,strong)UIVisualEffectView * effctView;//毛玻璃

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self createUI];

}

- (void)createUI{

self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

self.tableView.delegate = self;

self.tableView.dataSource = self;

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

//留出头部的imageView 的高度

self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);

[self.view addSubview:self.tableView];

self.topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,-200, self.view.frame.size.width, 200)];

_topImageView.image = [UIImage imageNamed:@"a.jpg"];

//照片要按照自己的比例去添加

_topImageView.contentMode = UIViewContentModeScaleAspectFill;

//对照片超出的部分要进行剪裁

_topImageView.clipsToBounds = YES;

[self.tableView addSubview:self.topImageView];

//添加毛玻璃效果

UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

UIVisualEffectView * effctView = [[UIVisualEffectView alloc]initWithEffect:blur];

effctView.frame = _topImageView.frame;

_effctView = effctView;

[self.tableView addSubview:_effctView];

}

//监控tableview的滚动,下来的时候让图片变大,毛玻璃效果减弱

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

//向下的话,为负数

CGFloat off_y = scrollView.contentOffset.y;

NSLog(@"-------%f",off_y);

CGFloat KHeight = [UIScreen mainScreen].bounds.size.height;

//下拉的时候超过照片的高度的时候

if (off_y < - 200) {

CGRect  frame = self.topImageView.frame;

//这里的思路就是改变顶部的照片的frame

self.topImageView.frame = CGRectMake(0, off_y, frame.size.width, -off_y);

self.effctView.frame = self.topImageView.frame;

//对应调整毛玻璃的效果

self.effctView.alpha = 1 + (off_y + 200)/KHeight;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 话说苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如下图的通知中心界面; 效果图:...
    Mr_董阅读 5,099评论 0 0
  • 首先, 简述一下我想实现的效果, 整个页面是以一个tableView为主的简单浏览页, tableView.hea...
    牵着螃蟹逛街走阅读 3,909评论 2 5
  • 前一段时间项目中用到毛玻璃效果,那时对UIBlurEffect类和 UIVisualEffectView这两个类做...
    WheatDen阅读 11,983评论 1 2
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 5,353评论 9 5
  • 天色渐暗 心也愈加冰冷 仅有的希望即要破灭 上天何故如此? 苦涩的笑在嘴角绽放 风吹来 脸上一片清凉 明知道不过是...
    花少颜阅读 1,369评论 0 0

友情链接更多精彩内容