代码之家  ›  专栏  ›  技术社区  ›  user4219465

全屏模糊视图

  •  2
  • user4219465  · 技术社区  · 10 年前

    当触摸“显示视图”按钮时,我希望在应用程序中显示模糊背景。我可以制作模糊背景,但不是全屏。它显示在选项卡栏和导航栏下。

    这是我的登录屏幕,也是我想要的( 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 回复  |  直到 10 年前
        1
  •  1
  •   soulshined    10 年前

    苹果为我们提供了一个复制模糊效果的绝佳资源。调查 UIBlurEffect

    typedef enum {
    UIBlurEffectStyleExtraLight,
    UIBlurEffectStyleLight,
    UIBlurEffectStyleDark 
    } UIBlurEffectStyle;
    
        2
  •  -2
  •   delavega66 JDGuide    10 年前

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

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

    编辑:

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

    蓝色BG.h:

    #import <UIKit/UIKit.h>
    
    @interface BlurBG : UIViewController
    - (void)showAlert:(UIViewController *)vc;
    - (void) ProgressUpdate ;
    @end
    

    蓝色BG.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];