2 exit door eXit_door 于 2013.06.07 10:38 提问

UISwipeGestureRecognizerDirection问题

在viewDidLoad 中设置:

 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:swipeGesture];

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    UISwipeGestureRecognizerDirection temp = recognizer.direction;
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        [self backCalendarPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        [self nextCalendarPressed:nil];
    }
}

但是recognizer.direction总是等于3,这样的结果我没有办法判断是向左滑动还是向右滑动。

1个回答

zhanglei5415
zhanglei5415   Rxr 2013.06.07 11:52
已采纳

在初始时设置确定一个初始状态.比如下一步的动作是向左滑. 设置direction

 [swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft]; 

只添加对向左滑的手势支持,而不是两个方向的支持.可加一个BOOL类型的变量来标识当前的滑动方向

BOOL isLeft=NO;

在处理滑动的事件处理中,根据当前方向重置direction

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    UISwipeGestureRecognizerDirection temp = recognizer.direction;
    if (!isLeft)
    {
        [swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];
        [self backCalendarPressed:nil];
    }
    else 
    {
        [swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self nextCalendarPressed:nil];
    }
    isLeft=!isLeft;
}
eXit_door
eXit_door 谢谢
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!