從底部淡入淡出動畫

常見的淡入淡出動畫。

1.創(chuàng)建baseView(子視圖),位置設(shè)置為屏幕下邊


- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100)];
        _baseView.backgroundColor = COLOR_B8;
    }
    return _baseView;
}

2.布局完成后,調(diào)用show方法

1)self背景色從clear 到某個指定顏色

2)baseView.frame到需要顯示的位置

- (void)show {
    _baseViewHeight = 49 * (_items.count + 1) + 5;
    self.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.35 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        self.baseView.frame = CGRectMake(0, SCREEN_HEIGHT-_baseViewHeight, SCREEN_WIDTH, _baseViewHeight);
    } completion:^(BOOL finished) {
        
    }];
}

3.點(diǎn)擊按鈕或者觸摸屏幕消失

- (void)clickItemBtnAction:(UIButton *)sender {
    NSInteger index = sender.tag - 200;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.35 animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.baseView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, _baseViewHeight);
    } completion:^(BOOL finished) {
        if (_delegate && [_delegate respondsToSelector:@selector(bottomView:selected:)]) {
            [_delegate bottomView:self selected:index];
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
    point = [self.baseView.layer convertPoint:point fromLayer:self.layer];
    if (![self.baseView.layer containsPoint:point]) {
        [self clickItemBtnAction:self.btnCancel];
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容