ios双边抽屉效果

@interface PathView : UIView

@property(nonatomic, assign) CGFloat leftWidth;
@property(nonatomic, assign) CGFloat rightWidth;

@end
#import "PathView.h"
#import "UIView+Additions.h"

@implementation PathView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupGestureRecognizer];
    }
    return self;
}

- (void)setupGestureRecognizer {
    UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self addGestureRecognizer:pan];
}

- (void)handlePan:(UIPanGestureRecognizer*)recognizer {
    CGPoint translation = [recognizer translationInView:self];
    
    CGFloat Xresult = translation.x + self.left;
    
    if (translation.x >= 0) {//向右
        if (self.left >= _leftWidth || Xresult >= _leftWidth) {
            self.frame = CGRectMake(_leftWidth, 0, self.width, self.height);
            return;
        }
    }
    else if (translation.x < 0) {//向左
        if (self.left <= -_rightWidth || Xresult <= -_rightWidth) {
            self.frame = CGRectMake(-_rightWidth, 0, self.width, self.height);
            return;
        }
    }
    
    self.left += translation.x;
    
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (self.left > _leftWidth / 2) {
            [self openLeft:YES openRight:NO];
        }
        
        else if (self.left < -(_rightWidth / 2)) {
            [self openLeft:NO openRight:YES];
        }
        
        else {
            [self openLeft:NO openRight:NO];
        }
    }
    
    //清空移动的距离
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
}

- (void)openLeft:(BOOL)left openRight:(BOOL)right {
    if (!left && !right) {
        [self moveView:CGRectMake(0, 0, self.width, self.height)];
        
    } else if (!left && right) {
        [self moveView:CGRectMake(-_rightWidth, 0, self.width, self.height)];
        
    } else if (left && !right) {
        [self moveView:CGRectMake(_leftWidth, 0, self.width, self.height)];
        
    }
}

- (void)moveView:(CGRect)frame {
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}

———————————————————分 割 线————————————————————————————————

上面是实现的具体内容,和上一篇的思路是相似的,所以不做多少注释,只不过把手势移到view里面,让view自己改变自己的frame;再有就是添加了两个属性,来指定左右两边各自抽屉抽出的宽度,某一边不需要,只要设置为0就好了。

转载于:https://my.oschina.net/u/574245/blog/390712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值