ios - 如何添加模糊背景和弹出 View ?

标签 ios objective-c uiview uivisualeffectview

我想在我的应用程序中有一个模糊的背景和 View 弹出窗口。但是当我点击我的按钮时,模糊背景和 View 没有出现......任何人都可以帮助我完成这个任务...... 我的代码是

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

最佳答案

问题

  • 为什么不显示模糊背景? - 因为你没有将它添加到 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;
    }];
  }];
}

关于ios - 如何添加模糊背景和弹出 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51241300/

相关文章:

objective-c - iOS 下载安全

ios - 使用 CAKeyframeAnimation 延迟 UIView 的动画开始?

ios - 授权应用程序时不再识别 Facebook 连接 publish_stream (iOS)

objective-c - 核心数据 NSManagedObject - 跟踪属性是否已更改

ios - iOS 7.1 中的 SDWebImage 崩溃

ios - 以编程方式从 UIView 加载 View Controller

ios - 通过 GMSMapView 拦截 UIView 上的触摸

ios - SwiftUI:关闭所有事件的工作 TableView

iOS swift : UIAccessibilitySpeechAttributeIPANotation

iphone - 使用 AFJSONRequestOperation