码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 毛玻璃效果

时间:2015-04-13 16:51:30      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

//

//  AMBlurView.h

//  blur

//

//  Created by Dubai Pinto Castillo on 7/3/15.

//  Copyright (c) 2013 Dubai All rights reserved.

//


#import <UIKit/UIKit.h>


@interface JCRBlurView : UIView


// Use the following property to set the tintColor. Set it to nil to reset.

@property (nonatomic, strong) UIColor *blurTintColor;


@end


JCRBlurView.m为:

//

//  AMBlurView.m

//  blur

//

//  Created by Dubai Pinto Castillo on 7/3/15.

//  Copyright (c) 2013 Dubai All rights reserved.

//


#import "JCRBlurView.h"


@interface JCRBlurView ()


@property (nonatomic, strong) UIToolbar *toolbar;


@end


@implementation JCRBlurView


- (instancetype)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];

    if (self) {

        [self setup];

    }

    return self;

}


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        [self setup];

    }

    return self;

}


- (instancetype)init

{

    self = [super init];

    if (self) {

        [self setup];

    }

    return self;

}


- (void)setup {

    // If we don‘t clip to bounds the toolbar draws a thin shadow on top

    [self setClipsToBounds:YES];

    

    if (![self toolbar]) {

        [self setToolbar:[[UIToolbar alloc] initWithFrame:[self bounds]]];

        [self.toolbar setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self insertSubview:[self toolbar] atIndex:0];

        

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_toolbar]|"

                                                                     options:0

                                                                     metrics:0

                                                                       views:NSDictionaryOfVariableBindings(_toolbar)]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_toolbar]|"

                                                                     options:0

                                                                     metrics:0

                                                                       views:NSDictionaryOfVariableBindings(_toolbar)]];

    }

}


- (void) setBlurTintColor:(UIColor *)blurTintColor {

    [self.toolbar setBarTintColor:blurTintColor];

}


@end


你创建自己的Controller:
首先引入JCRBlurView.h:

在你自己的controller.m里:

//

//  SecondViewController.m

//  磨平Demo

//

//  Created by Dubaion 15/4/13.

//  Copyright (c) 2015 Dubai. All rights reserved.

//


#import "SecondViewController.h"


#import "JCRBlurView.h"


@interface SecondViewController ()


@property (nonatomic,strong) JCRBlurView *blurView;



@property (strong, nonatomic) UIButton *btn;





@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    

    self.btn = [UIButton buttonWithType:(UIButtonTypeSystem)];

    self.btn.frame = CGRectMake(60, 80, 60, 40);

    [self.btn setTitle:@"毛玻璃" forState:(UIControlStateNormal)];

    [self.btn addTarget:self action:@selector(didBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:self.btn];

    

    

    UIImageView *iamgeView = [[UIImageView alloc] initWithFrame:self.view.bounds];

    iamgeView.image = [UIImage imageNamed:@"fish.jpg"];


    [self.view addSubview:iamgeView];

    [self setBlurView:[JCRBlurView new]];

   

    [[self blurView] setFrame:CGRectMake(20.f, 20.f, [self.view bounds].size.width-40.f, [self.view bounds].size.height-40.f)];

    [[self blurView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

    [self.view addSubview:[self blurView]];

    

}


- (void)didBtnAction:(UIButton *)sender

{

    NSLog(@"毛玻璃效果");

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


有一张背景图:效果为:
技术分享


iOS 毛玻璃效果

标签:

原文地址:http://blog.csdn.net/zhaoguodongios/article/details/45025169

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!