第一次滚动后uiscrollview方向不显示

时间:2015-02-05 12:30:59

标签: ios objective-c uiscrollview

我试图找出用户滚动方向或甚至可能的用户手势swipt

在uiscrollview上。

起初我尝试了这段代码:

 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeScreen:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[self.uiscroll addGestureRecognizer:swipe];

- (void)didSwipeScreen:(UISwipeGestureRecognizer *)gesture
{
    switch (gesture.direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"UISwipeGestureRecognizerDirectionUp");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"UISwipeGestureRecognizerDirectionDown");
            break;
        case UISwipeGestureRecognizerDirectionLeft:
             NSLog(@"UISwipeGestureRecognizerDirectionLeft");
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"UISwipeGestureRecognizerDirectionRight");
            break;
        default:
            break;
    }
}

但是当我挥动时没有任何事情发生。

所以即时尝试滚动拖动的内置方法:

 @property (nonatomic, assign) CGPoint lastContentOffset;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"first _lastContentOffset.x : %f",_lastContentOffset.x);

    _lastContentOffset.x = scrollView.contentOffset.x;
    _lastContentOffset.y = scrollView.contentOffset.y;

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"_lastContentOffset.x : %f",_lastContentOffset.x);
    NSLog(@"(int)scrollView.contentOffset.x: %d",(int)scrollView.contentOffset.x);
    if (_lastContentOffset.x < (int)scrollView.contentOffset.x) {
        NSLog(@"right");

    }
    else if (_lastContentOffset.x > (int)scrollView.contentOffset.x) {
        NSLog(@"left");

    }else if (_lastContentOffset.y<(int)scrollView.contentOffset.y){
        NSLog(@"up");

    }else if (_lastContentOffset.y>(int)scrollView.contentOffset.y){
        NSLog(@"down");


    }
}

第二种方法的问题是,它在第​​一次滚动后停止工作。

第一个滚动效果很好,但第二个滚动效果很好。

这是第一次和第二次右滚动的日志输出:

2015-02-05 15:57:09.675 app[4792:554076] first _lastContentOffset.x : 0.000000
2015-02-05 15:57:09.798 app[4792:554076] _lastContentOffset.x : 0.000000
2015-02-05 15:57:09.800 app[4792:554076] (int)scrollView.contentOffset.x: 8
2015-02-05 15:57:09.801 app[4792:554076] right
2015-02-05 15:57:10.422 app[4792:554076] first _lastContentOffset.x : 0.000000
2015-02-05 15:57:10.548 app[4792:554076] _lastContentOffset.x : 8.000000
2015-02-05 15:57:10.549 app[4792:554076] (int)scrollView.contentOffset.x: 8
2015-02-05 15:57:15.146 app[4792:554076] first _lastContentOffset.x : 8.000000
2015-02-05 15:57:15.365 app[4792:554076] _lastContentOffset.x : 8.000000

1 个答案:

答案 0 :(得分:0)

我想问题出在

UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

可悲的是,您必须创建2个手势才能使其正常工作。

滚动视图在手势和它具有的滚动之间混淆。