从底部淡入淡出动画

2017-05-18  本文已影响20人  秋S寂S

常见的淡入淡出动画。

1.创建baseView(子视图),位置设置为屏幕下边


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

2.布局完成后,调用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.点击按钮或者触摸屏幕消失

- (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];
    }
}

上一篇下一篇

猜你喜欢

热点阅读