上拉式弹窗的实现

  有些弹窗如果点击按钮的同时就出现一个弹窗就显得有的突兀,这次实现的效果是点击按钮的同时弹窗由下往上弹出来,添加了动画效果:

1、首先创建一个弹出的View类,

在HTAlertView.h中:

@property (nonatomic, strong) void (^backBtnActionBlock)();

@property (weak, nonatomic) IBOutlet UIView *myView;

@property(nonatomic,strong) UIControl *huiseControl;

- (void)showInView:(UIView *) view;

+ (instancetype)getSimpleView;

2、在HTAlertView.m中:

- (void)awakeFromNib{
    
    _huiseControl=[[UIControl alloc]initWithFrame:CGRectMake(0, 0, UIBounds.size.width, UIBounds.size.height)];
    _huiseControl.backgroundColor=RGBACOLOR(0, 0, 0, 0.4);
    [_huiseControl addTarget:self action:@selector(huiseControlClick) forControlEvents:UIControlEventTouchUpInside];
    _huiseControl.alpha=0;
    self.backgroundColor = [UIColor whiteColor];
}

+ (instancetype)getSimpleView{
    
    NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"HTAlertView" owner:nil options:nil];
    return [objs lastObject];
}
- (void)showInView:(UIView *) view {

    if (_huiseControl.superview==nil) {
        [view addSubview:_huiseControl];
    }
    [UIView animateWithDuration:0.2 animations:^{
        _huiseControl.alpha=1;
    }];
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = 0.2f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self.layer addAnimation:animation forKey:@"animation1"];
    self.frame = CGRectMake(0,view.frame.size.height - 355, [UIScreen mainScreen].bounds.size.width, 355);
    [view addSubview:self];

}
- (void)hideInView {
    
    self.hidden = YES;
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = 0.2f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self.layer addAnimation:animation forKey:@"animtion2"];
    [UIView animateWithDuration:0.2 animations:^{
        _huiseControl.alpha=0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_huiseControl removeFromSuperview];
    }];
   
}
- (IBAction)btnClick:(UIButton *)sender {
    
    if (self.backBtnActionBlock) {
        
        self.backBtnActionBlock();
    }
}

-(void)huiseControlClick{
    
    [self hideInView];
}

其中

#define UIBounds [[UIScreen mainScreen] bounds] //window外框大小
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:a]

最后在viewController中要弹窗的button实现方法即可

- (void)popupButtonClick
{
    HTAlertView *alert = [HTAlertView getSimpleView];
    
    alert.backBtnActionBlock = ^(){
      
        NSLog(@"sdgvet4wfdscx");
    };
    
    [alert showInView:self.view];
}

 其中有代码块可以回调得到View中的按钮触发

转载于:https://www.cnblogs.com/h-tao/p/5635377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值