简体   繁体   English

检测UICollectionView中的滑动

[英]Detect swiping in UICollectionView

I need to perform a specific action when the user swipes the uicollectionview. 我需要在用户滑动uicollectionview时执行特定操作。 I built it in a way that each cell captures the full screen. 我以每个单元格捕获全屏的方式构建它。

I tried those ways: 我试过这些方法:

A. scrollViewDidEndDecelerating A. scrollViewDidEndDecelerating

# pragma UIScrollView
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"detecting scroll");
    for (UICollectionViewCell *cell in [_servingTimesCollectionView visibleCells]) {
        NSIndexPath *indexPath = [_servingTimesCollectionView indexPathForCell:cell];
        CGPoint scrollVelocity = [scrollView.panGestureRecognizer velocityInView:_servingTimesCollectionView];
        if (scrollVelocity.x > 0.0f)
            NSLog(@"going right");
        else if (scrollVelocity.x < 0.0f)
            NSLog(@"going left");
    }
}

But the scrollVelocity returns null. scrollVelocity返回null。 The method is being called. 正在调用该方法。

B. UISwipeGestureRecognizer B. UISwipeGestureRecognizer

In ViewDidLoad of my UIViewController which delegates to UICollectionViewDataSource and UIGestureRecognizerDelegate I added: 在我的UIViewController ViewDidLoad ,它委托给UICollectionViewDataSourceUIGestureRecognizerDelegate我添加了:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
swipeRight.numberOfTouchesRequired = 1;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
swipeRight.numberOfTouchesRequired = 1;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

[_servingTimesCollectionView addGestureRecognizer:swipeRight];
[_servingTimesCollectionView addGestureRecognizer:swipeLeft];

and the followings in the UiViewController: 以及UiViewController中的以下内容:

#pragma mark - UISwipeGestureRecognizer Action
-(void)didSwipeRight: (UISwipeGestureRecognizer*) recognizer {
    NSLog(@"Swiped Right");
}

-(void)didSwipeLeft: (UISwipeGestureRecognizer*) recognizer {
    NSLog(@"Swiped Left");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"Asking permission");
    return YES;
}

But none are called. 但没有一个被称为。

What is wrong? 怎么了? I am developing for ios7 我正在为ios7开发

You are not setting the delegate of the gestures: 您没有设置手势的代理:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
    swipeRight.delegate = self;
    swipeRight.numberOfTouchesRequired = 1;
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
    swipeLeft.delegate = self;
    swipeLeft.numberOfTouchesRequired = 1;
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

i was trying to do same thing with you (by full size cell frames in a UICollectionView). 我试图与你做同样的事情(通过UICollectionView中的全尺寸单元格框架)。 Just delegating with self is not sufficient enought, because UICollectionView may has it's own gesture recognizer. 仅使用self委托是不够的,因为UICollectionView可能拥有它自己的手势识别器。 So below trick was worked in may case. 所以下面的伎俩在5个案例中有效。

    override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = .red

    let leftGR = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeHandler_Left(gestureRecognizer:)))
    leftGR.numberOfTouchesRequired = 1
    leftGR.direction = .left
    leftGR.delegate = self

    self.view.addGestureRecognizer(leftGR)
}

@objc func swipeHandler_Left(gestureRecognizer : UISwipeGestureRecognizer!) {
    if gestureRecognizer.state == .ended {
        // Perform action.
        print("***Free to Delete Old Cell")
    }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

sorry for my swifty answer, pls try to translate objc :) 对不起我的回答,请尝试翻译objc :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM