简体   繁体   English

全屏模糊视图

[英]Full Screen Blur View

I want to show a blur background in my app when touch the "Show View" button. 触摸“显示视图”按钮时,我想在我的应用程序中显示模糊的背景。 I can make blur background but it's not full screen. 我可以使背景模糊,但不是全屏。 it's shown under the tab bar and navigation bar. 它显示在标签栏和导航栏下方。

This is my login screen and what i want ( SCLAlertView ) 这是我的登录屏幕以及我想要的( SCLAlertView

在此处输入图片说明

But we can not add custom view to this library. 但是我们不能将自定义视图添加到该库中。 So i want to make a custom view and add a progress bar to this view. 因此,我想创建一个自定义视图并向该视图添加进度条。

在此处输入图片说明

 @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;

when i touch a button it's calling this method 当我触摸按钮时,就调用此方法

-(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];
}

Apple has given us a wonderful resource to replicate a blurred effect. 苹果为我们提供了复制模糊效果的绝佳资源。 Look into UIBlurEffect 调查UIBlurEffect

typedef enum {
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark 
} UIBlurEffectStyle;

I think you can use FXBlurView .. It's useful library. 我认为您可以使用FXBlurView ..它是有用的库。 After import this library, you can add a new View (or you can create dynamically). 导入该库后,您可以添加一个新的视图(或可以动态创建)。 Viewcontroller class type must be FXBlurView. Viewcontroller类的类型必须为FXBlurView。 You can find a little example below. 您可以在下面找到一个小例子。

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

Edit: 编辑:

If you don't want to use a library, you have to make your own blur background view. 如果您不想使用库,则必须制作自己的模糊背景视图。 I copied your code and edit it. 我复制了您的代码并对其进行了编辑。 you can find it below. 您可以在下面找到它。

BlurBG.h: BlurBG.h:

#import <UIKit/UIKit.h>

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

BlurBG.m: 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

Calling BlurBG: 调用BlurBG:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM