OC一句代码实现直播间界面滑动效果

效果图

2016-12-07 15_43_36.gif

外界可这样调用

- (UIView *)coverView {
    if (_coverView == nil) {
        [XYCoverView coverViewWithSuperView:self.contentView block:^(XYCoverView * _Nonnull view) {
            _coverView = view;
            view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.1];
        }];
        
    }
    return _coverView;
}


以下是对此控件的封装GitHub

.h文件

#import 

NS_ASSUME_NONNULL_BEGIN

@interface XYCoverView : UIView

/**
 * @explain 创建一个XYCoverView实例的对象
 *
 * @param   superView  父控件
 * @param   block  回调实例的对象
 */
+ (void)coverViewWithSuperView:(UIView *)superView block:(void(^)(XYCoverView *view))block;
@end

NS_ASSUME_NONNULL_END

.m文件

#import "XYCoverView.h"

@interface XYCoverView ()

@end

@implementation XYCoverView

+ (void)coverViewWithSuperView:(UIView *)superView block:(void(^)(XYCoverView *view))block {
    
    if (superView) {
        
        XYCoverView *view = [[self alloc] init];
        [superView addSubview:view];
        view.frame = superView.bounds;
        view.backgroundColor = [UIColor orangeColor];
        
        // 给自己添加手势
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(panGestureOnSelf:)];
        [view addGestureRecognizer:panGesture];
        
        // 给父控件添加点按手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(tapGestureOnSuperView:)];
        [superView addGestureRecognizer:tapGesture];
        UIPanGestureRecognizer *panGesture1 = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(panGestureOnSuperView:)];
        [superView addGestureRecognizer:panGesture1];
        
        if (block) {
            block(view);
        }
        
    }
   
}



#pragma mark - Event
- (void)tapGestureOnSuperView:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:0.2 animations:^{
        
        self.frame = self.superview.frame;
    }];
    
}

- (void)panGestureOnSelf:(UIPanGestureRecognizer *)pan
{
    
    // 取出偏移量
    CGPoint offset = [pan translationInView:self];
    // 计算当前coverView的frame
    self.frame = [self frameWithOffsetX:offset.x];
    // 复位
    [pan setTranslation:CGPointZero inView:self];
    
    // 当向右移动的隐藏rightView
    if (self.frame.origin.x > 0) {
        // 向右
    } else if (self.frame.origin.x < 0) {
        CGRect frame = self.frame;
        frame.origin.x = 0;
        self.frame = frame;
        // 向左
    }
    
    CGFloat target = 0;
    // 当手指松开时,做自动定位
    if (pan.state == UIGestureRecognizerStateEnded) {
        // 手指离开屏幕
        if (self.frame.origin.x> CGRectGetWidth(self.superview.bounds) * 0.3) {
            target = CGRectGetWidth(self.frame);
        } else if (CGRectGetMaxX(self.frame) < CGRectGetWidth(self.superview.bounds) * 0.3) {
            target = -CGRectGetWidth(self.superview.frame);;
        }
        
        CGFloat offsetX = target - self.frame.origin.x;
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = [self frameWithOffsetX:offsetX];
        }];
    }
    
}


- (void)panGestureOnSuperView:(UIPanGestureRecognizer *)pan {
    
    // 取出手指在view上的偏移量
    CGPoint offset = [pan translationInView:pan.view];
    
    //    NSLog(@"%f", offset.x);
    
    // 由于coverView上面也有了pan手势,根据事件传递,coverView在显示时会直接响应pan手势,而不会传递给父控件self.view,除非coverView不可以接受事件,所有当self.view可以响应pan事件时,coverView肯定不在显示着
    
    if (offset.x < 0) {
        CGFloat target = 0;
        CGFloat offsetX = target - self.frame.origin.x;
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = [self frameWithOffsetX:offsetX];
        }];
        
    }
}


- (CGRect)frameWithOffsetX:(CGFloat)offsetX {
    
    CGRect frame = self.frame;
    frame.origin.x += offsetX;
    
    frame.size.height = self.superview.bounds.size.height;
    return frame;
}


@end

你可能感兴趣的:(OC一句代码实现直播间界面滑动效果)