全屏模糊视图

时间:2015-02-24 07:20:19

标签: ios background blur

我想在触摸"显示视图"时在我的应用中显示模糊背景按钮。我可以模糊背景,但它不是全屏。它显示在标签栏和导航栏下方。

这是我的登录界面和我想要的内容(SCLAlertView

enter image description here

但我们无法向此库添加自定义视图。所以我想创建一个自定义视图并为此视图添加一个进度条。

enter image description here

 @property (nonatomic, strong) UIView *ContentView;
 @property (nonatomic, strong) UIImageView *BgView;


_BgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];

[_BgView setBackgroundColor:[UIColor blackColor]];
_BgView.userInteractionEnabled = YES;
_BgView.alpha = 1.0;
_BgView.tag=7001;

_ContentView = [[UIView alloc] init];
_ContentView.tag=7002;

[self.view addSubview:_ContentView];

_ContentView.backgroundColor = [UIColor whiteColor];
_ContentView.layer.cornerRadius = 2.0f;
_ContentView.layer.masksToBounds = YES;
_ContentView.layer.borderWidth = 0.5f;

当我触摸按钮时,它会调用此方法

-(void)BlurBg{

    float width = self.view.frame.size.width-20;
    float height= 120.0;
    float x     = (self.view.frame.size.width-width)/2;
    float y     = (self.view.frame.size.height-height)/2;

    _ContentView.frame = CGRectMake(x, y, width, height);
    UIButton *OkBtn = [[UIButton alloc] initWithFrame:CGRectMake((_ContentView.frame.size.width-(_IcerikView.frame.size.width/3))/2, ((_ContentView.frame.size.height-20)/3)*2, _ContentView.frame.size.width/3, 40)];
    OkBtn.backgroundColor=[UIColor greenColor];
    [OkBtn setTitle:@"OK" forState:UIControlStateNormal];
    OkBtn.layer.cornerRadius = 1;
    OkBtn.clipsToBounds = YES;
    [OkBtn addTarget:self action:@selector(Sonuc) forControlEvents:UIControlEventTouchUpInside];

    [_ContentView addSubview:OkBtn];

    [self.view addSubview:_BgView];
    [self.view addSubview:_ContentView];
}

-(void) Sonuc{
    [[self.view viewWithTag:7001] removeFromSuperview];
    [[self.view viewWithTag:7002] removeFromSuperview];
}

2 个答案:

答案 0 :(得分:3)

Apple为我们提供了一个很好的资源来复制模糊效果。查看UIBlurEffect

typedef enum {
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark 
} UIBlurEffectStyle;

答案 1 :(得分:0)

我认为你可以使用FXBlurView ..它是有用的库。导入此库后,您可以添加新视图(或者可以动态创建)。 Viewcontroller类类型必须是FXBlurView。你可以在下面找到一个小例子。

 @property (nonatomic, weak) IBOutlet FXBlurView *blurView;
 ...
 self.blurView.blurRadius = 40; // you can set 0 to 100

修改:

如果您不想使用库,则必须创建自己的模糊背景视图。我复制了你的代码并进行了编辑。你可以在下面找到它。

BlurBG.h:

#import <UIKit/UIKit.h>

@interface BlurBG : UIViewController
- (void)showAlert:(UIViewController *)vc;
- (void) ProgressUpdate ;
@end

BlurBG.m:

#import "BlurBG.h"
#import "UIImage+ImageEffects.h"
#import <AVFoundation/AVFoundation.h>

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define KEYBOARD_HEIGHT 80
#define PREDICTION_BAR_HEIGHT 40

@interface BlurBG ()
@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *OkBtn;
@property UIProgressView *prog;
@property (nonatomic) CGFloat backgroundOpacity;
@property UILabel *labelTitle;
@property UITextView *viewText;
@end

@implementation BlurBG

CGFloat kWindowWidth;
CGFloat kWindowHeight;
CGFloat kTextHeight;
CGFloat kSubTitleHeight;

#pragma mark - Initialization

- (id)initWithCoder:(NSCoder *)aDecoder
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                               reason:@"NSCoding not supported"
                             userInfo:nil];
}

-(instancetype) init{
    self = [super init];
    if (self)
    {
        kWindowWidth = [UIScreen mainScreen].bounds.size.width-50;
        kWindowHeight = ([UIScreen mainScreen].bounds.size.height/5);

        _OkBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWindowWidth-25,5,20,20)];
        _OkBtn.backgroundColor=[UIColor grayColor];
        [_OkBtn setTitle:@"X" forState:UIControlStateNormal];
        _OkBtn.layer.cornerRadius = 1;
        _OkBtn.clipsToBounds = YES;
        [_OkBtn addTarget:self action:@selector(fadeOut) forControlEvents:UIControlEventTouchUpInside];

        _prog = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];

        _labelTitle = [[UILabel alloc] init];
        _viewText = [[UITextView alloc] init];
        _contentView = [[UIView alloc] init];

        [self.view addSubview:_contentView];

        [_contentView addSubview:_labelTitle];
        [_contentView addSubview:_viewText];
        [_contentView addSubview:_prog];
        [_contentView addSubview:_OkBtn];

    // Content View
        _contentView.layer.cornerRadius = 5.0f;
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.borderWidth = 0.5f;

        _labelTitle.numberOfLines = 1;
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.font = [UIFont fontWithName:@"HelveticaNeue" size:20.0f];

    // View text
        _viewText.editable = NO;
        _viewText.allowsEditingTextAttributes = YES;
        _viewText.textAlignment = NSTextAlignmentCenter;
        _viewText.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];

        _backgroundView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.userInteractionEnabled = YES;

        _contentView.backgroundColor = [UIColor whiteColor];
        _labelTitle.textColor = UIColorFromRGB(0x4D4D4D);
        _viewText.textColor = UIColorFromRGB(0x4D4D4D);
        _contentView.layer.borderColor = UIColorFromRGB(0xCCCCCC).CGColor;

    }
    return self;
}

-(void) ShowTitle:(UIViewController *)vc{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    self.view.alpha = 0;

    [self makeBlurBackground];
    _backgroundView.frame = vc.view.bounds;

    _labelTitle.text = @"Title";
    _viewText.text = @"Description..";

    [window addSubview:_backgroundView];
    [window addSubview:self.view];
    [vc addChildViewController:self];

    [self fadeIn];
}

- (void)showAlert:(UIViewController *)vc {
    [self ShowTitle:vc];
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    CGSize sz = [UIScreen mainScreen].bounds.size;

    if (SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        if UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
        {
            CGSize ssz = sz;
            sz = CGSizeMake(ssz.height, ssz.width);
        }
    }

    CGRect newFrame = self.backgroundView.frame;
    newFrame.size = sz;
    self.backgroundView.frame = newFrame;

    CGRect r;
    if (self.view.superview != nil)
    {
        r = CGRectMake((sz.width-kWindowWidth)/2, (sz.height-kWindowHeight)/2, kWindowWidth, kWindowHeight/2);
    }
    else
    {
        r = CGRectMake((sz.width-kWindowWidth)/2, -kWindowHeight, kWindowWidth, kWindowHeight);
    }

    self.view.frame = r;

    _contentView.frame = CGRectMake(0,0, kWindowWidth, kWindowHeight);
    _labelTitle.frame = CGRectMake((kWindowWidth-(kWindowWidth-10))/2, 5, kWindowWidth-10,28);
    _viewText.frame = CGRectMake((kWindowWidth-(kWindowWidth-10))/2, 8+(_labelTitle.frame.size.height), kWindowWidth-10,28);
    _prog.frame = CGRectMake((kWindowWidth-(kWindowWidth-10))/2, 65, kWindowWidth-10,28);
}

- (void)fadeIn
{
    self.backgroundView.alpha = 0.0f;
    self.view.alpha = 0.0f;

    [UIView animateWithDuration:0.3f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     self.backgroundView.alpha = _backgroundOpacity;
                     self.view.alpha = 1.0f;
                 }
                 completion:^(BOOL completed){


    [self performSelectorOnMainThread:@selector(ProgressUpdate) withObject:nil waitUntilDone:YES];
                     }];
    }

    - (void)fadeOut
    {
    [UIView animateWithDuration:0.3f animations:^{
        self.backgroundView.alpha = 0.0f;
        self.view.alpha = 0.0f;
    } completion:^(BOOL completed) {
        [self.backgroundView removeFromSuperview];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

- (void)makeBlurBackground
{
    UIImage *image = [UIImage convertViewToImage];
    UIImage *blurSnapshotImage = [image applyBlurWithRadius:5.0f
                                                  tintColor:[UIColor colorWithWhite:0.2f
                                                                              alpha:0.7f]
                                      saturationDeltaFactor:1.8f
                                                  maskImage:nil];

    _backgroundView.image = blurSnapshotImage;
    _backgroundView.alpha = 0.0f;
    _backgroundOpacity = 1.0f;
}

-(void) ProgressUpdate {
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf) {
            for (int i=0; i<100; i++) {
                [NSThread sleepForTimeInterval:0.1];
                float currentProgress = _prog.progress;
                NSLog(@"%f",currentProgress);
                [strongSelf.prog setProgress:currentProgress+0.01 animated:YES];
            };
        }
    });
}

@end

致电BlurBG:

BlurBG *bg = [[BlurBG alloc]init];
[bg showAlert:self];
[bg ProgressUpdate];