仿面包旅行个人中心下拉顶部背景放大高斯模糊效果

HeaderView.h

//
//  HeaderView.h
//  仿面包旅行个人中心
//
//  Created by [email protected] on 15/5/14.
//  Copyright (c) 2015年 wb145230. All rights reserved.
//

#import <UIKit/UIKit.h>

@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 [email protected] on 15/5/14.
//  Copyright (c) 2015年 wb145230. All rights reserved.
//

#import "HeaderView.h"
#import <Accelerate/Accelerate.h>

@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 [email protected] on 15/5/14.
//  Copyright (c) 2015年 wb145230. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "HeaderView.h"

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) HeaderView *headerView;

@end

ViewController.m

//
//  ViewController.m
//  仿面包旅行个人中心
//
//  Created by [email protected] 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博客园看到本文,请点击查看原文.

时间: 2024-08-24 20:13:01

仿面包旅行个人中心下拉顶部背景放大高斯模糊效果的相关文章

仿手机QQ列表支持下拉,上滑,滑动删除

一般安卓程序员都知道下拉刷新主键用 com.handmark.pulltorefresh 网站:https://github.com/chrisbanes/Android-PullToRefresh/ 滑动删除用 fortysevendeg 的 swipelistview 但是要实现QQ列表功能,需要把两者叠加起来,我尝试了一下,发现有Bug, fortysevendeg的代码有点复杂,放弃了,后来自己搞了一个,为了方便广大 安卓程序员,我把整个demo贡献出来. 下载地址:http://pan

css 文字有下拉 但是背景始终不变

background:url(images/mainBG.gif) no-repeat fixed 120px 120px;属性可以解决... fixed 使得背景固定,热后下拉的话就始终是这个背景.

Android 下拉刷新,上滑加载更多

底部上拉效果 public class ListViewFooter extends LinearLayout { public final static int STATE_NORMAL = 0; public final static int STATE_READY = 1; public final static int STATE_LOADING = 2; private Context mContext; private View mContentView; private View

[ PHP+jQuery ] ajax 多级联动菜单的应用:电商网站的用户地址选择功能 ( 一 ) - 传统下拉菜单

/** jQuery version: 1.8.3 Author: 小dee Date: 2014.11.5 */ 说明:分析其他网站的图片较多,可以在目录跳过直接看本文 demo . 目录: 其他网站分析 亚马逊 淘宝 京东 当当 本文案例 demo1 demo2 [ 后面的博文再写 ] demo3 [ 后面的博文再写 ] 惯例,先看看他山之石,选择了四家比较大的电商网站:亚马逊.淘宝.京东.当当,看看它们的地址联动菜单是怎么做的. 1. 亚马逊[返回目录][下一节:淘宝] 图1 默认界面 说

ScrollView下拉视图放大

在网上找了好多下拉Scrollview图片放大的效果,结果看了一下都不太靠谱,后来自己想了一个办法解决了这个问题.跟大家分享一下. 其实就是对Scrollview下拉的监听将视图放大.不管是View还是ImageView放大就ok了.主要代码如下: /** 手指放下的监听*/ float initTouchY=0; mScrollView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, Motio

使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突

使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种冲突 如果你还在为处理滑动冲突而发愁,那么你需要静下心来看看这边文章,如果你能彻底理解这篇文章中使用的技术,那么,一切滑动冲突的问题解决起来就轻而易举了:先扔一个最终实现的效果图 先分析下效果图中实现的功能点 顶部下拉时背景图形成视差效果 上拉时标题栏透明切换显示 底部实现TabLayout+ViewPager+Fragment+RecyclerV

纯css+js下拉菜单实例代码

纯css+js下拉菜单实例代码 分享一个css+js下拉菜单代码,js+css实现的简单下拉菜单,兼容性不错. 例子:<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <

JavaScript下拉菜单的例子分享

css+js下拉菜单 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server&q

Android头像下拉缩放动效

头像下拉缩放动效 头像下拉缩放这个在IOS中很常见,最近在Github上也看到了类似的效果,所以决定把它集成到我现在做的项目中去. Github上的开源地址:https://github.com/Frank-Zhu/PullZoomView 先上2张效果图 PullToZoomView的使用 这个开源框架的使用主要用到的是PullToZoomListViewEx和PullToZoomScrollViewEx的2个类库,PullToZoomListViewEx这个是ListView的下拉效果,暂时