如何在 UIScrollView 中实现 scrollViewDidScroll [英] How to implement scrollViewDidScroll in UIScrollView

查看:55
本文介绍了如何在 UIScrollView 中实现 scrollViewDidScroll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,当我在 UIScrollView 的子类中调用 scrollViewDidScroll 方法时,没有任何反应.这是我的代码:

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

推荐答案

in

- (id)initWithFrame:(CGRect)frame

添加

self.delegate = self;

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

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

scrollview.delegate = self;

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

of course, you must implements the delegate method

scrollViewDidScroll:

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

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

@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>

这篇关于如何在 UIScrollView 中实现 scrollViewDidScroll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆