拖动手势设置边界
2021-04-08 本文已影响0人
开米肉丝酱
- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
CGPoint translation = [panGesture translationInView:panGesture.view.superview];
CGFloat space = 20;
// Figure out where the user is trying to drag the view.
CGPoint newCenter = CGPointMake(panGesture.view.center.x+ translation.x,
panGesture.view.center.y + translation.y);// 限制屏幕范围:
newCenter.y = MAX(panGesture.view.frame.size.height/2, newCenter.y);
newCenter.y = MIN(panGesture.view.superview.frame.size.height - panGesture.view.frame.size.height/2, newCenter.y);
newCenter.x = MAX(panGesture.view.frame.size.width/2+space, newCenter.x);
newCenter.x = MIN(panGesture.view.superview.frame.size.width - panGesture.view.frame.size.width/2 - space,newCenter.x);
panGesture.view.center = newCenter;
[panGesture setTranslation:CGPointZero inView:panGesture.view.superview];
// Don't need any logic for ended/failed/canceled states
}