iOS开发中常用的手势---边缘手势

 

说明:以下方法是开发中使用的方法,有什么不对的或者好的方法,请多多指教!

 

此处的边缘手势是用来控制左侧抽屉视图的弹出以及收回。

添加手势 :

  1. 页面上有多个手势时需要遵循 UIGestureRecognizerDelegate 协议
 1 @interface HomePageViewController ()<LeftDrawerViewDelegate,UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate,UIGestureRecognizerDelegate>{
 2     
 3     LeftDrawerViewViewController *_leftDrawerVC;
 4     
 5     UITableView *_tableView;
 6     
 7     UIView *_headeView;
 8     
 9     //轮播图详情展示URL数组
10     NSMutableArray *_urlArr;
11     
12     BOOL _leftDrawerState;
13     
14     UIView *_maskView;
15     
16     
17 }

2.添加边缘手势

 1  //添加边缘手势
 2     UIScreenEdgePanGestureRecognizer *screenEdgeGesOut = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenGesOut:)];
 3     screenEdgeGesOut.edges = UIRectEdgeLeft;
 4     
 5     [self.view addGestureRecognizer:screenEdgeGesOut];
 6     
 7     UIScreenEdgePanGestureRecognizer *screenEdgeGesIn = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenGesIn:)];
 8     screenEdgeGesIn.edges = UIRectEdgeRight;
 9     
10     [self.view addGestureRecognizer:screenEdgeGesIn];

3.实现手势中的方法以及协议中的方法

//控制抽屉视图弹出
- (void)screenGesOut:(UIScreenEdgePanGestureRecognizer *)ges{ UIView *tempUV = [self.view.window viewWithTag:DRAWERTAG]; if (_leftDrawerState == NO) { _leftDrawerState = YES; _maskView = [[UIView alloc]initWithFrame:self.view.frame]; [self.view addSubview:_maskView]; _maskView.backgroundColor = [UIColor lightGrayColor]; _maskView.alpha = 0.3; [UIView animateWithDuration:0.3 animations:^{ CGPoint ptDrawer = tempUV.center; ptDrawer.x += kSCREENWIDTH *3/4; tempUV.center = ptDrawer; // tabbarcontroller视图动 CGPoint ptTabbar = self.tabBarController.view.center; ptTabbar.x += kSCREENWIDTH *3/4; self.tabBarController.view.center = ptTabbar; }]; } }
//控制抽屉视图收回
- (void)screenGesIn:(UIScreenEdgePanGestureRecognizer *)ges{ UIView *tempUV = [self.view.window viewWithTag:DRAWERTAG]; if(_leftDrawerState == YES){ _leftDrawerState = NO; [_maskView removeFromSuperview]; [UIView animateWithDuration:0.3 animations:^{ CGPoint ptDrawer = tempUV.center; ptDrawer.x -= kSCREENWIDTH *3/4; tempUV.center = ptDrawer; // tabbarcontroller视图动 CGPoint ptTabbar = self.tabBarController.view.center; ptTabbar.x -= kSCREENWIDTH *3/4; self.tabBarController.view.center = ptTabbar; }]; } } //页面有多个手势时 需要遵循协议-> UIGestureRecognizerDelegate 以及实现协议中的方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }

 

posted @ 2016-08-17 08:53  寂寞的倔强  Views(2743)  Comments(0Edit  收藏  举报