首页 > 移动平台 > 详细

ios双边抽屉效果

时间:2015-03-24 13:14:47      阅读:145      评论:0      收藏:0      [点我收藏+]
@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就好了。

ios双边抽屉效果

原文:http://my.oschina.net/u/574245/blog/390712

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!