iOS8模糊效果UIVisualEffectView的使用

iOS8模糊效果UIVisualEffectView的使用

技术分享

效果:

技术分享

源码:

//
//  ViewController.m
//  EffectView
//
//  Created by XianMingYou on 15/3/14.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIVisualEffectView *vibrancyEffectView;
@property (nonatomic, strong) UIScrollView       *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加测试用view
    [self.view addSubview:self.scrollView];
    
    // 添加模糊用view
    [self.view addSubview:self.effectView];
    
    // 添加额外的view(用contentView)
    [self.effectView.contentView addSubview:self.vibrancyEffectView];
}

@synthesize scrollView = _scrollView;
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator   = NO;
        _scrollView.bounces                        = NO;
        UIImageView *imageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"color.jpg"]];
        _scrollView.contentSize = imageView.image.size;
        [_scrollView addSubview:imageView];
    }
    
    return _scrollView;
}

@synthesize effectView = _effectView;
- (UIVisualEffectView *)effectView {
    if (_effectView == nil) {
        // 添加模糊效果
        _effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
        _effectView.userInteractionEnabled = NO;
        _effectView.frame                  = CGRectMake(0, 0, 320, 200);
        _effectView.center                 = self.view.center;
    }
    
    return _effectView;
}

@synthesize vibrancyEffectView = _vibrancyEffectView;
- (UIVisualEffectView *)vibrancyEffectView {
    if (_vibrancyEffectView == nil) {
        // 初始化文本
        UILabel *label      = [[UILabel alloc] initWithFrame:self.effectView.bounds];
        label.text          = @"YouXianMing";
        label.font          = [UIFont systemFontOfSize:40.f];
        label.textAlignment = NSTextAlignmentCenter;
        
        // 需要与作用的effectView的效果一致
        _vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)self.effectView.effect]];
        _vibrancyEffectView.frame = self.effectView.bounds;
        
        // 注意,是用的contentView添加label
        [self.vibrancyEffectView.contentView addSubview:label];
    }
    
    return _vibrancyEffectView;
}

@end

测试用图片:

技术分享

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。