How to implement scrollViewDidScroll in UIScrollView

最后都变了- 提交于 2019-12-12 09:41:40

问题


I'm having a problem where when I call the scrollViewDidScroll method in my subclass of UIScrollView nothing happens. Here is my code:

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

回答1:


in

- (id)initWithFrame:(CGRect)frame

add

self.delegate = self;

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

scrollview.delegate = self;

of course, you must implements the delegate method

scrollViewDidScroll:

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

@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>



回答2:


For iOS10, SWift 3.0 implement scrollViewDidScroll on UIScrollView

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.
}
}



回答3:


Step 1: Create Delegate for UIViewController Class:

  @interface ViewController : UIViewController <UIScrollViewDelegate>

Step 2: Then add Delegate for your UIScrollView object:

  scrollview.delegate = self;

Step 3: Implements Delegate methods as follow:

 - (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 ScrollViewDidScroll method into our Objective-C Class.



来源:https://stackoverflow.com/questions/14310332/how-to-implement-scrollviewdidscroll-in-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!