本文實例為年夜家分享了IOS完成抽屜後果的全體代碼,供年夜家參考,詳細內容以下
IOS完成簡略單純抽屜後果,代碼:
@interface ViewController () { UIView* _leftView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _leftView = [[UIView alloc] init]; //把左邊邊的view先隱蔽 _leftView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height); _leftView.backgroundColor = [UIColor greenColor]; [self.view addSubview:_leftView]; UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithtarget:self action:@selector(handlePan:)]; [self.view addGestureRecognizer:pan]; } - (void)handlePan:(UIPanGestureRecognizer*) recognizer { CGPoint translation = [recognizer translationInView:self.view]; //增量後的x坐標地位 CGFloat Xresult = translation.x + _leftView.frame.origin.x; //向右 if (translation.x >= 0) { //leftView已全體拉出,則沒法再向右 if (_leftView.frame.origin.x >= 0 || Xresult >= 0) { _leftView.frame = CGRectMake(0, 0, 200, self.view.frame.size.height); return; } } else if (translation.x < 0) {//向左 //leftView已全體發出,則沒法再向左 if (_leftView.frame.origin.x <= -200 || Xresult <= -200) { _leftView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height); return; } } CGRect frame = _leftView.frame; frame.origin.x += translation.x; _leftView.frame = frame; //清空挪動的間隔,這是症結 [recognizer setTranslation:CGPointZero inView:recognizer.view]; //做彈回後果,以中軸為界線 if (recognizer.state == UIGestureRecognizerStateEnded) { if (_leftView.frame.origin.x > -100) { [self closeView:NO]; } else { [self closeView:YES]; } } } - (void)closeView:(BOOL)close { if (close) { [self moveView:CGRectMake(-200, 0, 200, self.view.frame.size.height)]; } else { [self moveView:CGRectMake(0, 0, 200, self.view.frame.size.height)]; } } - (void)moveView:(CGRect)frame { [UIView animateWithDuration:0.3 animations:^{ _leftView.frame = frame; } completion:^(BOOL finished) { }]; }
IOS完成雙邊抽屜後果,代碼:
#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抽屜後果。
【iOS完成簡略單純抽屜後果、雙邊抽屜後果】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!