UIScrollViewDelegate 不工作

发布于 12-19 19:05 字数 1374 浏览 4 评论 0原文

您好,我正在尝试在用户滚动应用程序时触发一个功能。

这是我的代码

,我定义了一个实现 UIScrollViewDelegate 的类 MyViewController

@interface CircleViewController : UIViewController <UIScrollViewDelegate> {
  UIScrollView *scroll;
}

@property (retain, nonatomic) UIScrollView * scroll;

@end

然后,在 .m 中,在我编写的 ViewDidLoad 中:

- (void)viewDidLoad {
[super viewDidLoad];
self.scroll = [[UIScrollView alloc] init];
scroll.delegate = self;
[scroll setBackgroundColor:[UIColor whiteColor]];

NSInteger nbView = 3;


scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];

for(int i = 0; i < nbView; i++) {
    CGFloat xOrigin = i * self.view.frame.size.height;

    UIView * vue = [[UIView alloc] initWithFrame:CGRectMake(0, xOrigin,     self.view.frame.size.width, self.view.frame.size.height)];
    vue.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:vue];
    [vue release];

}
scroll.contentSize = CGSizeMake(self.view.frame.size.width,   self.view.frame.size.height * nbView);
[scroll setScrollEnabled:YES];
[self.view addSubview:scroll];  

并且我定义了触发器:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"test");
} 

当我运行应用程序时,我可以滚动,但永远不会调用scrollViewDidScroll 。

Hello I'm trying to trigger a function when the user is scrolling the app.

Here is my code

I defined a class MyViewController which implements the UIScrollViewDelegate

@interface CircleViewController : UIViewController <UIScrollViewDelegate> {
  UIScrollView *scroll;
}

@property (retain, nonatomic) UIScrollView * scroll;

@end

Then, in the .m, in the ViewDidLoad I wrote :

- (void)viewDidLoad {
[super viewDidLoad];
self.scroll = [[UIScrollView alloc] init];
scroll.delegate = self;
[scroll setBackgroundColor:[UIColor whiteColor]];

NSInteger nbView = 3;


scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];

for(int i = 0; i < nbView; i++) {
    CGFloat xOrigin = i * self.view.frame.size.height;

    UIView * vue = [[UIView alloc] initWithFrame:CGRectMake(0, xOrigin,     self.view.frame.size.width, self.view.frame.size.height)];
    vue.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:vue];
    [vue release];

}
scroll.contentSize = CGSizeMake(self.view.frame.size.width,   self.view.frame.size.height * nbView);
[scroll setScrollEnabled:YES];
[self.view addSubview:scroll];  

And I define the trigger :

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"test");
} 

When I run the app, I am able to scroll, but the scrollViewDidScroll never gets called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端2024-12-26 19:05:01

您分配滚动视图两次可能是导致问题的原因......
您只需要分配一次,然后设置委托。

you are allocating the scroll view twice may be that what is causing the problem...
you just need to alloc it once and then set the delegate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文