如何在UIScrollView中实现scrollViewDidScroll功能

5

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

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

#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);
}

3
你是否添加了scrollView.delegate=self;? - gg13
1
好的,我会在想要添加scrollView的页面的viewDidLoad中进行初始化并放置它,然后将其添加到那里。尝试只将其添加到想要添加它的页面的viewDidLoad中。 - gg13
实际上,只需尝试将其放入initWithFrame中,可能会在那里起作用。 - gg13
为什么你想在应用程序委托中使用滚动视图? 我认为那不是一个好选择。 - Harshal Bhavsar
3个回答

6
对于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.
}
}

5

- (id)initWithFrame:(CGRect)frame

添加

self.delegate = self;

或者在AppDelegate.m文件中,在滚动视图初始化后,添加以下代码:

scrollview.delegate = self;

当然,您必须实现代理方法。
scrollViewDidScroll:

请不要忘记在AppDelegate.h中添加以下代码。
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>

4
步骤1:为UIViewController类创建代理。
  @interface ViewController : UIViewController <UIScrollViewDelegate>

步骤2:然后为您的UIScrollView对象添加Delegate:
  scrollview.delegate = self;

步骤3:按照以下方式实现委托方法:
 - (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
 }

这是您需要的。借助上述3个步骤,您可以将ScrollViewDidScroll方法集成到我们的Objective-C类中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接