首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pressGestureRecognizer和touchesBegan

pressGestureRecognizer和touchesBegan
EN

Stack Overflow用户
提问于 2013-02-06 21:17:50
回答 2查看 3.8K关注 0票数 3

我有以下问题。我正在使用UILongPressGestureRecognizer将UIView设置为“切换模式”。如果UIView处于“切换模式”,则用户能够在屏幕上拖动UIView。为了在屏幕上拖动UIView,我使用了touchesBegantouchesMovedtouchesEnded方法。

它可以工作,但是:我必须抬起手指才能拖动它,因为touchesBegan方法已经被调用,因此不会再次调用,因此我不能在屏幕上拖动UIView

UILongPressGestureRecognizer被触发后,有没有办法手动调用touchesBegan (UILongPressGestureRecognizer更改BOOL值,只有当BOOL值设置为YES时,touchesBegan才起作用)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-06 21:42:52

UILongPressGestureRecognizer是一个连续的手势识别器,所以不要求助于touchesMovedUIPanGestureRecognizer,只需检查UIGestureRecognizerStateChanged,例如:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [self.view addGestureRecognizer:gesture];
}

- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // user held down their finger on the screen

        // gesture started, entering the "toggle mode"
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        // user did not lift finger, but now proceeded to move finger

        // do here whatever you wanted to do in the touchesMoved
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // user lifted their finger

        // all done, leaving the "toggle mode"
    }
}
票数 10
EN

Stack Overflow用户

发布于 2013-02-06 21:38:44

我建议你使用UIPanGestureRecognizer,因为它是一种推荐的拖拽手势。

  1. 您可以配置min。和最大。平移所需的触摸次数,使用以下属性:

maximumNumberOfTouches

minimumNumberOfTouches

  • You可以处理像开始、改变和结束这样的状态,就像为所需的状态设置动画一样。

  • 使用下面的方法将点转换成你想要的UIView。

- (void)setTranslation:(CGPoint)translation inView:(UIView *)view

示例:

代码语言:javascript
复制
1. You have to use a global variable to retain the old frame. Get this in UIGestureRecognizerStateBegan.
2. When the state is UIGestureRecognizerStateChanged. You can use the

-(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{ if(panGesture.state==UIGestureRecognizerStateBegan){ //保留原始状态}else if(panGesture.state==UIGestureRecognizerStateChanged){ translatedPoint=panGesture translationInView:self.view;//在这里你可以设法获得新的拖动点。}}

  1. 阻力的速度。基于速度,您可以提供动画来显示UIView的弹跳

- (CGPoint)velocityInView:(UIView *)view

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14730056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档