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

时间:2022-07-09 10:53:52

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

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

添加手势 :

  1. 页面上有多个手势时需要遵循 UIGestureRecognizerDelegate 协议
 @interface HomePageViewController ()<LeftDrawerViewDelegate,UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate,UIGestureRecognizerDelegate>{

     LeftDrawerViewViewController *_leftDrawerVC;

     UITableView *_tableView;

     UIView *_headeView;

     //轮播图详情展示URL数组
NSMutableArray *_urlArr; BOOL _leftDrawerState; UIView *_maskView; }

2.添加边缘手势

  //添加边缘手势
UIScreenEdgePanGestureRecognizer *screenEdgeGesOut = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenGesOut:)];
screenEdgeGesOut.edges = UIRectEdgeLeft; [self.view addGestureRecognizer:screenEdgeGesOut]; UIScreenEdgePanGestureRecognizer *screenEdgeGesIn = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenGesIn:)];
screenEdgeGesIn.edges = UIRectEdgeRight; [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 */;
tempUV.center = ptDrawer; // tabbarcontroller视图动
CGPoint ptTabbar = self.tabBarController.view.center;
ptTabbar.x += kSCREENWIDTH */;
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 */;
tempUV.center = ptDrawer; // tabbarcontroller视图动
CGPoint ptTabbar = self.tabBarController.view.center;
ptTabbar.x -= kSCREENWIDTH */;
self.tabBarController.view.center = ptTabbar;
}];
}
} //页面有多个手势时 需要遵循协议-> UIGestureRecognizerDelegate 以及实现协议中的方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;
}