博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
仿面包旅行个人中心下拉顶部背景放大高斯模糊效果
阅读量:4972 次
发布时间:2019-06-12

本文共 7125 字,大约阅读时间需要 23 分钟。

HeaderView.h

////  HeaderView.h//  仿面包旅行个人中心////  Created by wb145230@163.com on 15/5/14.//  Copyright (c) 2015年 wb145230. All rights reserved.//#import 
@interface HeaderView : UIView@property(nonatomic, strong) UIScrollView *imageScrollView;@property(nonatomic, strong) UIImageView *imageView; //背景图片@property(nonatomic, strong) UIImageView *imageBackgroundView; //要改变的背景图片/** * 改变顶部view的大小和高斯效果 * * @param offset scrollview滑动的记录 */-(void)updateHeaderView:(CGPoint) offset;@end

 

HeaderView.m

////  HeaderView.m//  仿面包旅行个人中心////  Created by wb145230@163.com on 15/5/14.//  Copyright (c) 2015年 wb145230. All rights reserved.//#import "HeaderView.h"#import 
@implementation HeaderView- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.imageScrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; [self addSubview:self.imageScrollView]; UIImage *image = [UIImage imageNamed:@"header_bg"]; //高斯的背景图片 self.imageBackgroundView = [[UIImageView alloc] initWithFrame:self.imageScrollView.bounds]; [self setBlurryImage:image]; self.imageBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self.imageBackgroundView.contentMode = UIViewContentModeScaleAspectFill; [self.imageScrollView addSubview:self.imageBackgroundView]; //原图 self.imageView = [[UIImageView alloc] initWithFrame:self.imageScrollView.bounds]; self.imageView.image = image; self.imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self.imageView.contentMode = UIViewContentModeScaleAspectFill; [self.imageScrollView addSubview:self.imageView]; } return self;}/** * 通过scrollview的滑动改变顶部view的大小和高斯效果 * * @param offset scrollview下滑的距离 */-(void)updateHeaderView:(CGPoint) offset { if (offset.y < 0) { CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); CGFloat delta = fabs(MIN(0.0f, offset.y)); rect.origin.y -= delta; rect.size.height += delta; self.imageScrollView.frame = rect; self.clipsToBounds = NO; self.imageView.alpha = fabs(offset.y / (2 * CGRectGetHeight(self.bounds) / 3)); }}/** * 高斯图片 * * @param originalImage 需要高斯的图片 */- (void)setBlurryImage:(UIImage *)originalImage { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage *blurredImage = [self blurryImage:originalImage withBlurLevel:0.9]; dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.alpha = 0.0; self.imageBackgroundView.image = blurredImage; }); }); }/** * 高斯背景 * * @param image 需要高斯模糊的图片 * @param blur 高斯模糊的值 * * @return */- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur { if ((blur < 0.0f) || (blur > 1.0f)) { blur = 0.5f; } int boxSize = (int)(blur * 100); boxSize -= (boxSize % 2) + 1; CGImageRef img = image.CGImage; vImage_Buffer inBuffer, outBuffer; vImage_Error error; void *pixelBuffer; CGDataProviderRef inProvider = CGImageGetDataProvider(img); CFDataRef inBitmapData = CGDataProviderCopyData(inProvider); inBuffer.width = CGImageGetWidth(img); inBuffer.height = CGImageGetHeight(img); inBuffer.rowBytes = CGImageGetBytesPerRow(img); inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData); pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img)); outBuffer.data = pixelBuffer; outBuffer.width = CGImageGetWidth(img); outBuffer.height = CGImageGetHeight(img); outBuffer.rowBytes = CGImageGetBytesPerRow(img); error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); if (error) { NSLog(@"error from convolution %ld", error); } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, CGImageGetBitmapInfo(image.CGImage)); CGImageRef imageRef = CGBitmapContextCreateImage (ctx); UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; //clean up CGContextRelease(ctx); CGColorSpaceRelease(colorSpace); free(pixelBuffer); CFRelease(inBitmapData); CGColorSpaceRelease(colorSpace); CGImageRelease(imageRef); return returnImage;}@end

 

ViewController.h

////  ViewController.h//  仿面包旅行个人中心////  Created by wb145230@163.com on 15/5/14.//  Copyright (c) 2015年 wb145230. All rights reserved.//#import 
#import "HeaderView.h"@interface ViewController : UIViewController
@property(nonatomic, strong) UITableView *tableView;@property(nonatomic, strong) HeaderView *headerView;@end

 

ViewController.m

////  ViewController.m//  仿面包旅行个人中心////  Created by wb145230@163.com on 15/5/14.//  Copyright (c) 2015年 wb145230. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];        self.view.backgroundColor = [UIColor whiteColor];        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];    self.tableView.dataSource = self;    self.tableView.delegate = self;    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;    self.tableView.separatorColor = [UIColor clearColor];    self.tableView.showsVerticalScrollIndicator = NO;        self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];    self.tableView.tableHeaderView = self.headerView;    [self.view addSubview:self.tableView];    }-(void)scrollViewDidScroll:(UIScrollView *)scrollView {    [self.headerView updateHeaderView:scrollView.contentOffset];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 10;}- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];    }    cell.selectionStyle = UITableViewCellSelectionStyleNone;    return cell;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}@end

 

效果

 

如果你不是在wb145230博客园看到本文,.

 

转载于:https://www.cnblogs.com/wb145230/p/4570052.html

你可能感兴趣的文章
highcharts 图表实例
查看>>
highcharts曲线图
查看>>
extjs动态改变样式
查看>>
宏定义
查看>>
笔记:git基本操作
查看>>
生成php所需要的APNS Service pem证书的步骤
查看>>
JavaWeb之JSON
查看>>
HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
查看>>
windows平台上编译mongdb-cxx-driver
查看>>
optionMenu-普通菜单使用
查看>>
2016-2017-2点集拓扑作业[本科生上课时]讲解视频
查看>>
【MemSQL Start[c]UP 3.0 - Round 1 C】 Pie Rules
查看>>
Ognl中“%”、“#”、“$”详解
查看>>
我对应用软件——美团的看法
查看>>
执行了的程序,才是你的程序.
查看>>
struts2.x + Tiles2.x读取多个xml 配置文件
查看>>
表单校验之datatype
查看>>
python第六篇文件处理类型
查看>>
ubuntu16系统磁盘空间/dev/vda1占用满的问题
查看>>
grid网格布局
查看>>