gpt4 book ai didi

ios - UIScrollView中如何实现scrollViewDidScroll

转载 作者:可可西里 更新时间:2023-11-01 05:04:42 25 4
gpt4 key购买 nike

我遇到一个问题,当我在我的 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);
}

最佳答案

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

关于ios - UIScrollView中如何实现scrollViewDidScroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14310332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com