子类化 UIScrollview 不会触发委托方法

Subclassing UIScrollview does not trigger delegate methods

大家好,我正在尝试在 ViewController 中为 UIScrollview 在单独的 class 中实现 scrollViewDidScroll。不触发该方法...感谢任何帮助。

C8MyProfileScrollView.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface C8MyProfileScrollView : NSObject <UIScrollViewDelegate>
@property (nonatomic, assign) id<UIScrollViewDelegate> myOwnDelegate;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
@end

NS_ASSUME_NONNULL_END

C8MyProfileScrollView.m

#import "C8MyProfileScrollView.h"

    @implementation C8MyProfileScrollView
    
-(id)init{
    self = [super init];

    self.myOwnDelegate = self;
    return self;
}

    
    
    - (void) scrollViewDidScroll:(UIScrollView *)scrollView {
        NSLog(@"scrolling in seperate class");
    }

@end

在我的viewcontroller中我是这样设置的

C8MyProfileScrollView *profileTopScrollView = [[C8MyProfileScrollView alloc]initWithFrame:self.scrollView.frame];
self.scrollView.delegate=profileTopScrollView.myOwnDelegate;

您误解了 delegate/protocol 模式。

快速示例...

MyScrollDelegate.h

#import <UIKit/UIKit.h>

@interface MyScrollDelegate : NSObject <UIScrollViewDelegate>
@end

MyScrollDelegate.m

#import "MyScrollDelegate.h"

@implementation MyScrollDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%@ - in %s", [NSValue valueWithCGPoint:scrollView.contentOffset], __PRETTY_FUNCTION__);
}

@end

示例视图控制器...


ExtScrollDelegateViewController.h

#import <UIKit/UIKit.h>

@interface ExtScrollDelegateViewController : UIViewController
@end

ExtScrollDelegateViewController.m

#import "ExtScrollDelegateViewController.h"
#import "MyScrollDelegate.h"

@interface ExtScrollDelegateViewController ()
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) MyScrollDelegate *myDelegateObj;
@end

@implementation ExtScrollDelegateViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _scrollView = [UIScrollView new];
    _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    
    UILabel *v1 = [UILabel new];
    v1.backgroundColor = [UIColor greenColor];
    v1.text = @"Label v1";
    v1.translatesAutoresizingMaskIntoConstraints = NO;
    
    UILabel *v2 = [UILabel new];
    v2.backgroundColor = [UIColor greenColor];
    v2.text = @"Label v2";
    v2.translatesAutoresizingMaskIntoConstraints = NO;

    _scrollView.backgroundColor = [UIColor yellowColor];
    
    [_scrollView addSubview:v1];
    [_scrollView addSubview:v2];
    [self.view addSubview:_scrollView];

    UILayoutGuide *g = [self.view safeAreaLayoutGuide];
    UILayoutGuide *cg = [_scrollView contentLayoutGuide];
    
    [NSLayoutConstraint activateConstraints:@[
        
        [_scrollView.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
        [_scrollView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:20.0],
        [_scrollView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-20.0],
        [_scrollView.heightAnchor constraintEqualToConstant:200.0],
        
        [v1.topAnchor constraintEqualToAnchor:cg.topAnchor constant:8.0],
        [v1.leadingAnchor constraintEqualToAnchor:cg.leadingAnchor constant:8.0],

        [v2.topAnchor constraintEqualToAnchor:v1.bottomAnchor constant:48.0],
        [v2.leadingAnchor constraintEqualToAnchor:v1.trailingAnchor constant:480.0],
        
        [v2.trailingAnchor constraintEqualToAnchor:cg.trailingAnchor constant:-8.0],
        [v2.bottomAnchor constraintEqualToAnchor:cg.bottomAnchor constant:-8.0],

    ]];
    
    _myDelegateObj = [MyScrollDelegate new];
    _scrollView.delegate = _myDelegateObj;
    
}

@end

滚动滚动视图会输出很多这样的行:

ProjectName[76541:16417260] NSPoint: {109.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {120.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {131, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {141.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {151.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {161, 0} - in -[MyScrollDelegate scrollViewDidScroll:]