简体   繁体   English

UIScrollView委托方法冲突

[英]UIScrollView Delegate methods clashing

I am using the following two UIScrollView delegate methods to call another method in my UIViewController: 我正在使用以下两个UIScrollView委托方法在UIViewController中调用另一个方法:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // do something
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    // do something
}

I've tried various different ways of calling //do something but either end up with both not being called, or both delegate methods being called, calling //do something twice in certain situations. 我尝试了各种不同的调用//做某事的方式,但是要么最终都没有被调用,要么都被调用了两个委托方法,在某些情况下调用//做某事两次。 For example: 例如:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if(![scrollView isDecelerating] && ![scrollView isDragging]){
        //do something
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(!decelerate){
        //do something
    }
}

So with the above if I scroll and let it slow to a stop, it calls scrollViewDidEndDecelerating:, but if I scroll and stop it with a tap it calls both scrollViewDidEndDragging: and scrollViewDidEndDecelerating: 因此,对于上述内容,如果我滚动并使其慢下来,它将调用scrollViewDidEndDecelerating :,但是如果我轻按滚动并使其停止,则它将同时调用scrollViewDidEndDragging:和scrollViewDidEndDecelerating:

I want it to call one or the other... is there something I can do with scrollViewDidEndDecelerating: and the scrollView object to stop this double method call? 我希望它调用一个或另一个...可以用scrollViewDidEndDecelerating做些什么:和scrollView对象停止此双重方法调用吗?

// very simple // 很简单

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    if(!decelerate){
         // Do something 
    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
       // Do something 
}

The scroll view delegate does its job. 滚动视图委托完成其工作。 You can not prevent it from happening. 您无法阻止它的发生。 But with a simple logic you can achieve what you are trying to do. 但是,通过简单的逻辑,您可以实现您想要做的事情。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    dragged = YES;
    // do something
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (!dragged) {

         // do something
    }

    dragged = NO;
}

This seems to work. 这似乎有效。 Close to EmptyStack's answer 接近EmptyStack的答案

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (!dragged) {
        //do something
    }

    dragged = NO;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    if(!decelerate){
        dragged = YES;
        //do something
    } else {
        dragged = NO;
    }
}

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

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