结合长按手势和拖动手势 [英] Combine longpress gesture and drag gesture together
问题描述
我正在移动我的观点
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[bubbleView[rightCnt] addGestureRecognizer:panRecognizer];
[panRecognizer release];
现在,我希望通过长按拖动来做同样的事情。
Now , I want to do same thing by drag with long press.
任何想法?
推荐答案
UILongPressGestureRecognizer
已经做了你想要的。看一下 UIGestureRecognizerState
属性。来自文档:
UILongPressGestureRecognizer
already does what you want for you. Take a look at the UIGestureRecognizerState
property. From the documentation:
长按手势是连续的。当指定时间段内按下允许手指数
(numberOfTouchesRequired)时,手势开始
(UIGestureRecognizerStateBegan)
(minimumPressDuration)且触摸不会超出
允许范围运动范围(allowableMovement)。每当手指移动时,手势
识别器转换到更改状态,
并且当任何手指
被解除时,它结束(UIGestureRecognizerStateEnded)。
Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.
因此,基本上在调用 UILongPressGestureRecognizer
选择器之后,您将收听UIGestureRecognizerStateBegan,UIGestureRecognizerStateChanged,UIGestureRecognizerStateEnded。在 UIGestureRecognizerStateChanged
期间继续更改视图框架。
So essentially after your UILongPressGestureRecognizer
selector is called you listen to UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, UIGestureRecognizerStateEnded. Keep changing your views frame during 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
}
}
这篇关于结合长按手势和拖动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!