【问题标题】:Create A Shadow(or blur) when user Click on UIbutton?当用户单击 UIbutton 时创建阴影(或模糊)?
【发布时间】:2017-02-09 11:12:11
【问题描述】:

我在按钮中有一个 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新手...

【问题讨论】:

  • 相反,如果不需要动画,您可以使用两个已选中且正常的图像并相应更改
  • 谢谢你的建议..

标签: ios objective-c uiview uiviewanimation


【解决方案1】:

您可以在背景中添加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){
             }];
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      这里是教程链接https://www.raywenderlich.com/84043/ios-8-visual-effects-tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 1970-01-01
        • 2019-01-10
        • 2015-09-17
        • 2021-11-24
        相关资源
        最近更新 更多