This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int scrollCount = 0;

    scrollin.text = [NSMutableString stringWithFormat:@"didScroll - %i",scrollCount];

    scrollCount++;
}


总是得到didScroll-0;
它不应该因为每次滚动结束时都会调用此方法

最佳答案

每次调用该方法时都会初始化scrollCount,这意味着它将始终为0,因此显示为0。如果希望scrollCount永久存在于函数中,则应将其设为静态。您可以执行以下操作:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    static int scrollCount = 0;
    scrollin.text = [NSMutableString stringWithFormat:@"didScroll - %i",scrollCount];
    scrollCount++;
}


这样,scrollCount将仅初始化一次,并且每次调用该方法时都会递增。

另一种方法是在某种类变量中跟踪scrollCount,但是如果您只在方法内部使用它,那对我来说是不好的做法。

10-08 12:15