需求:项目中要求tableView的高度随着手拖动的位置而改变如下图:
关键代码如下:
- (void)viewDidLoad{
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(toHandlePanAction:)];
[self.searchResultView addGestureRecognizer: panGestureRecognizer];
}
- (void)toHandlePanAction:(UIPanGestureRecognizer *)sender {
CGPoint point = [sender translationInView:self.view];
CGRect frame = self.searchResultView.frame;
frame.origin.y = frame.origin.y + point.y;
frame.size.height = frame.size.height - point.y;
self.searchResultView.frame = frame;
[ sender setTranslation: CGPointMake(0, 0) inView: self.view ];
}
//[sender translationInView:self.view] 返回的是距离上次拖动位置的x,y的偏差值
//[ sender setTranslation: CGPointMake(0, 0) inView: self.view ] 在拖动过程中会连续回调toHandlePanAction这个方法,
然后要重置上一次的拖动位置的偏移量为0,这样才能够连续拖动