将longpress手势和拖动手势组合在一起

我正在转移我的观点

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)]; [panRecognizer setMinimumNumberOfTouches:1]; [panRecognizer setMaximumNumberOfTouches:1]; [panRecognizer setDelegate:self]; [bubbleView[rightCnt] addGestureRecognizer:panRecognizer]; [panRecognizer release]; 

现在,我想通过长按拖动来做同样的事情。

任何想法?

UILongPressGestureRecognizer已经为你做了你想要的。 看看UIGestureRecognizerState属性。 从文档 :

长按手势是连续的。 当在指定的时间段(minimumPressDuration)按下允许的手指的数量(numberOfTouchesRequired)并且触摸不超过允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。 当手指移动时,手势识别器转换到改变状态,并且当任何手指抬起时手势识别器结束(UIGestureRecognizerStateEnded)。

所以基本上在你的UILongPressGestureRecognizerselect器被调用后,你可以听到UIGestureRecognizerStateBegan,UIGestureRecognizerStateChanged,UIGestureRecognizerStateEnded。 在UIGestureRecognizerStateChanged期间不断更改您的视图框架。

 - (void)moveRight:(UILongPressGestureRecognizer *)gesture { if(gesture.state == UIGestureRecognizerStateBegan) { //if needed do some initial setup or init of views here } else if(gesture.state == UIGestureRecognizerStateChanged) { //move your views here. [yourView setFrame:]; } else if(gesture.state == UIGestureRecognizerStateEnded) { //else do cleanup } } 
 @implementation MyViewController { CGPoint _priorPoint; } - (void)moveRight:(UILongPressGestureRecognizer *)sender { UIView *view = sender.view; CGPoint point = [sender locationInView:view.superview]; if (sender.state == UIGestureRecognizerStateChanged) { CGPoint center = view.center; center.x += point.x - _priorPoint.x; center.y += point.y - _priorPoint.y; view.center = center; } _priorPoint = point; } 

你不需要声明_priorPoint;

在我的情况下,我只想要视图水平移动,所以我只改变x坐标。

这是我的解决scheme:

  if (longpressGestRec.state == UIGestureRecognizerStateChanged) { UIView *view = longpressGestRec.view; // Location of the touch within the view. CGPoint point = [longpressGestRec locationInView:view]; // Calculate new X position based on the amount the gesture // has moved plus the size of the view we want to move. CGFloat newXLoc = (item.frame.origin.x + point.x) - (item.frame.size.width / 2); [item setFrame:CGRectMake(newXLoc, item.frame.origin.y, item.frame.size.width, item.frame.size.height)]; } 

在Swift中,可以使用下面的代码来实现

 class DragView: UIView { // Starting center position var initialCenter: CGPoint? override func didMoveToWindow() { super.didMoveToWindow() // Add longPress gesture recognizer let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(gesture:))) addGestureRecognizer(longPress) } // Handle longPress action func longPressAction(gesture: UILongPressGestureRecognizer) { if gesture.state == .began { guard let view = gesture.view else { return } initialCenter = gesture.location(in: view.superview) } else if gesture.state == .changed { guard let originalCenter = initialCenter else { return } guard let view = gesture.view else { return } let point = gesture.location(in: view.superview) // Calculate new center position var newCenter = view.center; newCenter.x += point.x - originalCenter.x; newCenter.y += point.y - originalCenter.y; // Update view center view.center = newCenter } else if gesture.state == .ended { ... } }