@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) { }]; }
———————————————————分 割 線————————————————————————————————atom
上面是實現的具體內容,和上一篇的思路是類似的,因此不作多少註釋,只不過把手勢移到view裏面,讓view本身改變本身的frame;再有就是添加了兩個屬性,來指定左右兩邊各自抽屜抽出的寬度,某一邊不須要,只要設置爲0就行了。code