ios view 上有个子viewheaderView 给headerView添加向上向下拖拽手势 view视图跟随拖拽
您可以使用UIPanGestureRecognizer来实现向上向下拖拽手势,并通过更改headerView的frame来实现视图跟随拖拽。以下是一个示例代码:
首先,创建一个UIPanGestureRecognizer并将其添加到headerView上:
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[headerView addGestureRecognizer:panGestureRecognizer];
然后定义handlePan:方法来处理拖拽手势:
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:self.view];
// 更新headerView的frame
CGRect frame = headerView.frame;
frame.origin.y += translation.y;
headerView.frame = frame;
[gestureRecognizer setTranslation:CGPointZero inView:self.view];
}
在handlePan:方法中,我们首先获取手势的移动距离translation,然后将这个距离添加到headerView的y坐标上,以实现视图随手势移动。
最后,通过调用setTranslation:inView:方法将手势的移动距离重置为零,以避免累积效果。
请注意,上述代码只处理垂直方向的拖拽手势,如果您还需要处理水平方向的拖拽手势,可以根据需要进行修改
原文地址: https://www.cveoy.top/t/topic/iMyT 著作权归作者所有。请勿转载和采集!