简体   繁体   English

UIView中的UIScrollView:以编程方式检测滚动

[英]UIScrollView in UIView: detecting scroll programmatically

I got a UIView containing an outlet to a UIScrollView. 我有一个UIView包含一个UIScrollView的出口。

I would like to detect when the view scroll. 我想检测视图何时滚动。

This is my code: 这是我的代码:

// interface
@interface MyView : UIView <UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
-(void)setup;

// implementation
@implementation MyView    
@synthesize scrollView;

-(void)setup {
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];

    scrollView.pagingEnabled = true;
    [scrollView setContentSize:CGSizeMake(30, 160)];
    scrollView.showsHorizontalScrollIndicator = false;
    scrollView.bounces = false;
    scrollView.scrollEnabled = true;

    [self addSubview:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"Point: %@", NSStringFromCGPoint(scrollView.contentOffset));
}

What am I missing? 我想念什么? Do I need to modify the storyboard to activate "scrollViewDidScroll"? 我是否需要修改情节提要以激活“ scrollViewDidScroll”? Can I do this programmatically? 我可以以编程方式执行此操作吗?

您需要在设置过程中设置scrollView的委托:

self.scrollView.delegate = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM