pressGestureRecognizer和touchesBegan

时间:2013-02-06 13:17:50

标签: ios cocoa-touch cocoa touchesbegan

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

它有效,但是:我必须抬起手指才能拖动它,因为touchesBegan方法已被调用,因此不会再次调用,因此我无法拖动UIView屏幕周围。

触发touchesBegan后,是否有办法手动调用UILongPressGestureRecognizer UILongPressGestureRecognizer更改BOOL值,touchesBegan仅在此BOOL设置为YES时才有效

2 个答案:

答案 0 :(得分:10)

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

- (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"
    }
}

答案 1 :(得分:0)

我建议您使用UIPanGestureRecognizer作为推荐的拖动手势。

  1. 您可以配置分钟。最多使用以下属性进行平移所需的触摸次数:

    <强> maximumNumberOfTouches

    <强> minimumNumberOfTouches

  2. 您可以处理Began,Changed和Ended等状态,例如为所需状态设置动画。

  3. 使用以下方法将该点转换为您想要的UIView。

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

    示例:

    1. 您必须使用全局变量来保留旧框架。在UIGestureRecognizerStateBegan中获取此信息。

    2. 当状态为UIGestureRecognizerStateChanged时。您可以使用

    3. -(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{
      
         if(panGesture.state==UIGestureRecognizerStateBegan){
           //retain the original state
         }else if(panGesture.state==UIGestureRecognizerStateChanged){
         CGPoint translatedPoint=[panGesture translationInView:self.view];
        //here you manage to get your new drag points.
        }
       }
      
    4. 阻力的速度。根据速度,您可以提供动画来显示UIView的弹跳

      - (CGPoint)velocityInView:(UIView *)view