如何添加模糊背景和弹出视图?

Posted

技术标签:

【中文标题】如何添加模糊背景和弹出视图?【英文标题】:How can I add a blur background and popup view? 【发布时间】:2018-07-09 08:41:34 【问题描述】:

我想在我的应用程序中有一个模糊的背景并查看弹出窗口。但是当我点击我的按钮时,没有出现模糊背景和视图......谁能帮我完成这个任务...... 我的代码是

- (IBAction)ButtonTouched:(id)sender 

if(!UIAccessibilityIsReduceTransparencyEnabled())
    self.view.backgroundColor = [UIColor clearColor];
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blureffectview = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    blureffectview.frame = self.view.bounds;
    blureffectview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //[self.view addSubview:blureffectview];
    [self.view insertSubview:_AnimatedViewForLoading aboveSubview:blureffectview];


else
    //self.view.backgroundColor = [UIColor blackColor];




UIView * AnimatedViewForLoading;
 //   [[UIView alloc] initWithFrame:CGRectMake(118, 318, 200, 150)];
self.AnimatedViewForLoading.frame = CGRectMake(118, 318, 200, 150);
[self.AnimatedViewForLoading setBackgroundColor:[UIColor blueColor]];
self.AnimatedViewForLoading.alpha = 0.0f;
//AnimatedViewForLoading.backgroundColor = UIColor.lightGrayColor;

self.AnimatedViewForLoading.transform = CGAffineTransformMakeScale(0.01, 0.01);
 [self.view addSubview:self.AnimatedViewForLoading];
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^

    self.AnimatedViewForLoading.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);

 completion:^(BOOL finished) 

    NSLog(@"oopssssss..........");
    [UIView animateWithDuration:0.3/2 animations:^
        self.AnimatedViewForLoading.transform = CGAffineTransformIdentity;
    ];
];

【问题讨论】:

【参考方案1】:

问题

为什么不出现模糊背景? - 因为你没有添加到self.view

//[self.view addSubview:blureffectview];

为什么AnimatedViewForLoading 没有出现? - 因为你在初始化之前将它添加到self.view 并且self.AnimatedViewForLoading.alpha 设置为0.0f

// self.AnimatedViewForLoading is hidden at this line
self.AnimatedViewForLoading.alpha = 0.0f;

// |_AnimatedViewForLoading| is initialized after this line
[self.view insertSubview:_AnimatedViewForLoading aboveSubview:blureffectview];

解决方案

- (IBAction)ButtonTouched:(id)sender 
  //  UIView * AnimatedViewForLoading;
  //   [[UIView alloc] initWithFrame:CGRectMake(118, 318, 200, 150)];
  self.AnimatedViewForLoading = [[UIView alloc] initWithFrame:CGRectMake(118, 318, 200, 150)];
  [self.AnimatedViewForLoading setBackgroundColor:[UIColor blueColor]];
  // self.AnimatedViewForLoading.alpha = 0.0f;
  //AnimatedViewForLoading.backgroundColor = UIColor.lightGrayColor;

  // if(!UIAccessibilityIsReduceTransparencyEnabled())
  // self.view.backgroundColor = [UIColor clearColor];
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blureffectview = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    blureffectview.frame = self.view.bounds;
    blureffectview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:blureffectview];
    [self.view insertSubview:_AnimatedViewForLoading aboveSubview:blureffectview];
  // else
    //self.view.backgroundColor = [UIColor blackColor];
  // 

  self.AnimatedViewForLoading.transform = CGAffineTransformMakeScale(0.01, 0.01);
  [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^

    self.AnimatedViewForLoading.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);

   completion:^(BOOL finished) 

    NSLog(@"oopssssss..........");
    [UIView animateWithDuration:0.3/2 animations:^
      self.AnimatedViewForLoading.transform = CGAffineTransformIdentity;
    ];
  ];

【讨论】:

不确定为什么需要检查UIAccessibilityIsReduceTransparencyEnabled,但可能是 if 语句中的代码没有运行。我更新了我的答案。你可以再试一次,让我知道发生了什么 还没有实现……所有视图控制器都变黑了,按钮消失了…… 您似乎还有其他问题。如果你可以将项目添加到问题中,或者我可以帮助你通过teamviewer调试它会更好 @RKios 你可以查看我更新的答案。你忘了初始化AnimatedViewForLoading 这里是 [URL](we.tl/7mO39fxZfC)

以上是关于如何添加模糊背景和弹出视图?的主要内容,如果未能解决你的问题,请参考以下文章

如何同时关闭和弹出视图控制器

如何设置推送和弹出视图控制器流程

如何在 iOS 13 中查看和弹出视图控制器

将子视图推送和弹出到堆栈的问题

Xcode 推送和弹出视图以保存数据

无法在主视图控制器和弹出视图之间传递消息