提问人:stefanosn 提问时间:2/3/2022 最后编辑:stefanosn 更新时间:2/5/2022 访问量:146
子类化 UIScrollview 不会触发委托方法
Subclassing UIScrollview does not trigger delegate methods
问:
大家好,我正在尝试在我的 ViewController 中为 UIScrollview 的单独类中实现 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
在我的视图控制器中,我是这样设置的
C8MyProfileScrollView *profileTopScrollView = [[C8MyProfileScrollView alloc]initWithFrame:self.scrollView.frame];
self.scrollView.delegate=profileTopScrollView.myOwnDelegate;
答:
1赞
DonMag
2/5/2022
#1
您误解了委托/协议模式。
快速示例...
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:]
评论
0赞
stefanosn
2/12/2022
我只是找时间测试它!效果很好!非常感谢,我很感激DonMag先生!
评论
scrollView
UIScrollView
profileTopScrollView
profileTopScrollView
scrollView
C8MyProfileScrollView
myOwnDelegate