毛玻璃效果随着tableView滑动清晰

来源:互联网 发布:半生缘 葛优 知乎 编辑:程序博客网 时间:2024/05/22 06:06

毛玻璃效果处理:

        我们的需求是毛玻璃效果(高斯模糊)随着tableView向下滑动变的清晰,主要问题是页面渲染太多,在处理毛玻璃效果的时候页面非常卡顿,(iphone6以下)

查了很多资料页没有什么效果,后来在android大神的指导下完成需求,说一下原理吧:

        做两张图片  一个是高斯模糊的   一个是清晰的    模糊的图片盖在清晰的上面  滑动tableView,改变模糊啊的那个透明度alpha,滑倒下面模糊的那个透明度就是0了,然后清晰的那个显示了,  tableView滑动改变alpha不卡,效果也达到了。

      由于公司项目保密,自己写了一个demo,欢迎读阅:

       

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)UIImageView *imageView1;

@property (nonatomic,strong)UIImageView *imageView2;

@property (nonatomic,strong)UIImage *image1;

@property (nonatomic,strong)UIImage *image2;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [selfcreatUI]; 

}


- (void)creatUI

{

    self.imageView1 = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,375,667)];

    self.image1 = [UIImageimageNamed:@"屏幕快照 2016-04-23上午10.36.46"];

    self.imageView1.image =self.image1;

    [self.viewaddSubview:self.imageView1];

    self.imageView1.image = [self.image1gaussBlur:0];

    

    self.imageView2 = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,375,667)];

    self.image2 = [UIImageimageNamed:@"屏幕快照 2016-04-23上午10.36.46"];

    self.imageView2.image =self.image2;

    [self.viewaddSubview:self.imageView2];

    self.imageView2.image = [self.image2gaussBlur:1];

    

    

    self.tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,375,667)style:UITableViewStylePlain];

    self.tableView.delegate =self;

    self.tableView.dataSource =self;

    self.tableView.backgroundColor = [UIColor clearColor];

    [self.viewaddSubview:self.tableView];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return5;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil)

    {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];

    }

    cell.backgroundColor = [UIColorclearColor];

    return cell;

}


#pragma mark - 模糊效果设置/分业显示

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

        CGFloat offset=scrollView.contentOffset.y;

        if (offset<0)

        {

            CGFloat alpha=((128+offset)/128);

            self.imageView2.alpha = alpha;

//         self.visualEffectView.alpha =  alpha ;

            NSLog(@"%lf",alpha);

        }

        else

        {

            self.imageView2.alpha =1;

        }

}


下面这句话是对image的一个分类,处理高斯模糊的

    self.imageView1.image = [self.image1 gaussBlur:0];

如还有不明白的请看代码

    http://download.csdn.net/detail/qq_20176153/9502400

       

1 0
原创粉丝点击