当前位置:首页 > 操作系统 > Ios

iOS 毛玻璃效果

//

//  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 = [superinitWithCoder:aDecoder];

    if (self) {

        [selfsetup];

    }

    return self;

}


- (instancetype)initWithFrame:(CGRect)frame {

    self = [superinitWithFrame:frame];

    if (self) {

        [selfsetup];

    }

    return self;

}


- (instancetype)init

{

    self = [superinit];

    if (self) {

        [selfsetup];

    }

    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:[selfbounds]]];

        [ self . toolbar setTranslatesAutoresizingMaskIntoConstraints:NO];

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

        

        [ self addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[_toolbar]|"

                                                                     options:0

                                                                     metrics:0

                                                                       views:NSDictionaryOfVariableBindings(_toolbar)]];

        [ self addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[_toolbar]|"

                                                                     options:0

                                                                     metrics:0

                                                                       views:NSDictionaryOfVariableBindings(_toolbar)]];

    }

}


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

    [self.toolbarsetBarTintColor: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 = [UIColorwhiteColor];

    

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

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

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

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

    [self.viewaddSubview:self.btn];

    

    

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

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


    [self.viewaddSubview:iamgeView];

    [selfsetBlurView:[JCRBlurViewnew]];

   

    [[selfblurView] setFrame:CGRectMake(20.f, 20.f, [self.viewbounds].size.width-40.f, [self.viewbounds].size.height-40.f)];

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

    [self.viewaddSubview:[selfblurView]];

    

}


- (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


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


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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!