ios 如何在 UIScrollView 中实现 scrollViewDidScroll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14310332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:50:34  来源:igfitidea点击:

How to implement scrollViewDidScroll in UIScrollView

iosobjective-cuikit

提问by colindunn

I'm having a problem where when I call the scrollViewDidScrollmethod in my subclass of UIScrollViewnothing happens. Here is my code:

我遇到了一个问题,当我scrollViewDidScroll在我的子类中调用该方法时UIScrollView什么也没有发生。这是我的代码:

AppDelegate.m

AppDelegate.m

#import "ScrollView.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    CGRect screenRect = [[self window] bounds];

    ScrollView *scrollView = [[ScrollView alloc] initWithFrame:screenRect];
    [[self window] addSubview:scrollView];
    [scrollView setContentSize:screenRect.size];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

ScrollView.m

滚动视图.m

#import "AppDelegate.h"
#import "ScrollView.h"

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSString *imageString = [NSString stringWithFormat:@"image"];
        UIImage *image = [UIImage imageNamed:imageString];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        [super addSubview:imageView];
    }
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%f", scrollView.contentOffset.y);
}

采纳答案by Tony Stark

in

- (id)initWithFrame:(CGRect)frame

add

添加

self.delegate = self;

or in AppDelegate.m,after scrollview inited, add this code

或在 AppDelegate.m 中,在滚动视图初始化后,添加此代码

scrollview.delegate = self;

of course, you must implements the delegate method

当然,你必须实现委托方法

scrollViewDidScroll:

and don't forgot add below code in AppDelegate.h

并且不要忘记在 AppDelegate.h 中添加以下代码

@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>

回答by ViJay Avhad

For iOS10, SWift 3.0 implement scrollViewDidScroll on UIScrollView

对于 iOS10,Swift 3.0 在 UIScrollView 上实现 scrollViewDidScroll

class ViewController: UIViewController, UIScrollViewDelegate{

//In viewDidLoad Set delegate method to self.

@IBOutlet var mainScrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.mainScrollView.delegate = self

}
//And finally you implement the methods you want your class to get.
func scrollViewDidScroll(_ scrollView: UIScrollView!) {
    // This will be called every time the user scrolls the scroll view with their finger
    // so each time this is called, contentOffset should be different.

    print(self.mainScrollView.contentOffset.y)

    //Additional workaround here.
}
}

回答by Suraj Sonawane

Step 1: Create Delegate for UIViewController Class:

第 1 步:为 UIViewController 类创建委托:

  @interface ViewController : UIViewController <UIScrollViewDelegate>

Step 2: Then add Delegate for your UIScrollViewobject:

第 2 步:然后为您的UIScrollView对象添加委托:

  scrollview.delegate = self;

Step 3: Implements Delegate methods as follow:

第 3 步:实现 Delegate 方法如下:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     // Do your stuff here...
     // You can also track the direction of UIScrollView here....
     // to check the y position use scrollView.contentOffset.y
 }

Here you go. With the help of above 3 steps you can integrate ScrollViewDidScrollmethod into our Objective-CClass.

干得好。借助以上 3 个步骤,您可以将ScrollViewDidScroll方法集成到我们的Objective-C类中。