UIView中屏幕点击删除遮罩

1.遮罩制作

重载layoutSubviews函数,要了解这个函数就看看UIView生命周期
_bottomHeight :下面弹出框的高度
CGAffineTransformMakeTranslation 定义运动方向负_bottomHeight表示0.3秒内移动这个距离

弹出结束后,即completion 后执行后面的内容

//在这个类里重载
@interface myActionSheet : UIView
@end

- (void)layoutSubviews {
    [super layoutSubviews];
        if (_bottomHeight > 0) {
            [UIView animateWithDuration:0.3 animations:^{
                self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
                self.bottomView.transform = CGAffineTransformMakeTranslation(0, -_bottomHeight);
            } completion:^(BOOL finished) {
                //自己想显示的内容
            }];
            _bottomHeight = 0;
        }
}

2.当遮罩住屏幕后,点一下立马消失

#pragma mark - Override Method 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

- (void)dismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.bottomView.transform = CGAffineTransformMakeTranslation(0,CGRectGetHeight(self.bottomView.frame));
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

另外一种制造延迟的方法

self.collectionView.transform = CGAffineTransformMakeTranslation(0.01, self.HeightFrame);
    [myGCDQueue executeInMainQueue:^{
        [UIView animateWithDuration:0.3 animations:^{
            self.collectionView.transform = CGAffineTransformMakeTranslation(0, 0);
            self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        }completion:^(BOOL finished) {
            
        }];
    }afterDelaySecs:0.1];
@interface myGCDQueue : NSObject

//主线程延时执行gcd
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;

@end

@implementation myGCDQueue
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec {
    
    NSParameterAssert(block);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * sec),dispatch_get_main_queue(), block);
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路途遥远gg

帮到你了就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值