当用户单击 UI 按钮时创建阴影(或模糊)?

Create A Shadow(or blur) when user Click on UIbutton?

我在 Buttom 中有一个 One Uibutton。当用户单击该按钮时,会显示一个 uiview 动画,如从按钮到顶部的弹出窗口。 这是我的代码

-(void)viewDidLoad 
{
    [super viewDidLoad];    
    self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);    
}

- (IBAction)animated:(id)sender
{
    if(_isAnimated)
    {
        [UIView animateWithDuration:0.3
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 520, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
                     }
                     completion:^(BOOL finished){
                     }];
       _isAnimated=NO;  
    }
    else
    {   
        [UIView animateWithDuration:0.3
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
                     }
                     completion:^(BOOL finished)
                     {

       }];
      _isAnimated=YES;
    }
}

Uiview 动画效果很好,但是当 uiview 出现时,背景视图会像 uiactionsheet 一样创建阴影(或模糊),并且在完成动画后背景视图是清晰的。 我不知道如何实施。 请帮助我 我是 ios 的新人...

在 viewdidload.add QuartzCore 框架中添加您的代码

#import <QuartzCore/CAAnimation.h>

self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
CABasicAnimation *animate = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
animate.fromValue = [NSNumber numberWithFloat:1.5];
animate.toValue = [NSNumber numberWithFloat:0.5];
animate.duration = 1.0;
[self.BuyButtonPopUpView.layer addAnimation:animation forKey:@"shadowOpacity"];
self.BuyButtonPopUpView.layer.shadowOpacity = 0.0;

您可以先添加 UIVisualEffectView,然后在此视图上添加弹出窗口。

这里有教程linkhttps://www.raywenderlich.com/84043/ios-8-visual-effects-tutorial

您可以在背景中添加一个 UIView 并在情节提要中将其 backgroundcolor 设置为黑色和 alpha 0.5。然后通过动画将其 alpha 从 0.5 更改为 0。您还可以添加一个 UITapGestureRecognizer 来检测点击它并在用户点击外部时关闭视图。

-(void)viewDidLoad 
{
    [super viewDidLoad];    
    self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000,     self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);    
}

- (IBAction)animated:(id)sender
{
    if(_isAnimated)
    {
        [UIView animateWithDuration:0.3
                      delay:0.0
                    options: UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 520, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
                     //showing background view here
                     self.viewBackground.alpha = 0.5;
                 }
                 completion:^(BOOL finished){
                 }];
        _isAnimated=NO;  
    }    
    else
    {   
         [UIView animateWithDuration:0.3
                      delay:0.0
                    options: UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
                     //dismissing background view here
                     self.viewBackground.alpha = 0.0;
                 }
                 completion:^(BOOL finished)
                 {

                 }];
         _isAnimated=YES;
    }
}

用于检测外面的水龙头并关闭您的视图:

创建 UITapGestureRecognizer

@interface ViewController ()
{
    UITapGestureRecognizer *tapGesture;
}

ViewDidLoad中初始化:

-(void)viewDidLoad 
{
    //defining your gesture method
    tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];

    //adding gesture recognizer on the background view
    [self.backgroundView addGestureRecognizer:tapGestureRecognizer];
}

在您的手势识别器方法中:

-(void)handleSingleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer
{
       [UIView animateWithDuration:0.3 
             animations:^{
                 //dismissing background view here
                 self.viewBackground.alpha = 0.0;
             }
             completion:^(BOOL finished){
             }];
}

希望这对您有所帮助。