respondsToSelector的作用

1、respondsToSelector 用来判断某一个方法时候实现(下面的代码意思:如果baseAPIdidStartRequest这个方法实现了,那么就去调用,防止出现异常)

    if ([self.delegate respondsToSelector:@selector(baseAPIdidStartRequest:)]) {
        [self.delegate baseAPIdidStartRequest:self];
    }

2、isKindOfClass:OBJ 用来判断时候是某个类或其子类的实例

3、isMemberOfClass:OBJ 用来判断是否是某个类的实例

    [self.delegate isKindOfClass:self];
    [self.delegate isMemberOfClass:self];


继续分析m文件,重点关注page Control // // TPBPageView.m // Pods // // Created by ming on 2025/8/4. // #import "TPBPageView.h" #import "TPBPageControl.h" #import "TPBDesignKit.h" #import "MASConstraint+TPBExpanded.h" @interface TPBPageView() <UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) TPBPageControl *pageControl; @property (nonatomic, strong) MASConstraint *contentHeightConstraint; @property (nonatomic, copy) NSArray<UIView *> *contentViewArray; @property (nonatomic, assign) CGFloat preWidth; @end @implementation TPBPageView - (void)setupInitialData { [super setupInitialData]; _layoutMode = TPBPageViewLayoutModeDefault; _showPageControl = YES; _contentHeight = 160; _preWidth = -1; } - (void)setupSubviews { [super setupSubviews]; [self addSubview:self.scrollView]; [self addSubview:self.pageControl]; } - (void)makeConstraint { [super makeConstraint]; [self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) { make.bottom.centerX.equalTo(self); }]; [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.leading.trailing.equalTo(self); if (self.showPageControl && self.layoutMode == TPBPageViewLayoutModeDefault) { make.bottom.equalTo(self.pageControl.mas_top); } else { make.bottom.equalTo(self); } self.contentHeightConstraint = make.height.equalTo(@(self.contentHeight)); }]; } - (void)bindActions { [super bindActions]; TPBWeakSelf self.pageControl.indexDidChangeCallback = ^(NSInteger value) { TPBStrongSelf [_self innerSwitchToPage:value animated:YES]; }; } - (void)layoutSubviews { [super layoutSubviews]; NSInteger count = self.contentViewArray.count; CGSize scrollSize = self.scrollView.frame.size; for (NSInteger i = 0; i < count; i++) { UIView *contentView = self.contentViewArray[i]; contentView.frame = CGRectMake(i * scrollSize.width, 0, scrollSize.width, scrollSize.height); } self.scrollView.contentSize = CGSizeMake(scrollSize.width * count, scrollSize.height); if (self.preWidth != scrollSize.width) { self.preWidth = scrollSize.width; self.scrollView.contentOffset = CGPointMake(self.pageControl.currentPage * scrollSize.width, 0); } } - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [UIScrollView new]; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _scrollView.pagingEnabled = YES; _scrollView.delegate = self; } return _scrollView; } - (TPBPageControl *)pageControl { if (!_pageControl) { _pageControl = [TPBPageControl new]; _pageControl.sameWidthPageIndicator = YES; _pageControl.contentInsets = UIEdgeInsetsMake(4, 12, 4, 12); } return _pageControl;; } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { CGFloat width = self.scrollView.frame.size.width; NSInteger targetPage = 0; if (width > 0) { targetPage = (*targetContentOffset).x / width; } if (targetPage == self.pageControl.currentPage) { return; } self.pageControl.currentPage = targetPage; [self updateContentHeight]; [self reportPageChanged]; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { } #pragma mark - Private - (void)reportPageChanged { NSInteger currentPage = self.pageControl.currentPage; if (currentPage >= self.contentViewArray.count) { return; } UIView *contentView = self.contentViewArray[currentPage]; if (self.delegate && [self.delegate respondsToSelector:@selector(tpbPageView:didChangeToPage:contentView:)]) { [self.delegate tpbPageView:self didChangeToPage:currentPage contentView:contentView]; } } - (void)updateContentHeight { NSInteger targetPage = self.pageControl.currentPage; if (targetPage >= self.contentViewArray.count) { return; } UIView *contentView = self.contentViewArray[targetPage]; CGFloat contentHeight = self.contentHeight; if (self.delegate && [self.delegate respondsToSelector:@selector(tpbPageView:heightForPage:contentView:)]) { CGFloat customHeight = [self.delegate tpbPageView:self heightForPage:targetPage contentView:contentView]; if (customHeight >= 0.01) { contentHeight = customHeight; } } if (contentHeight != self.contentHeightConstraint.tpbConstant) { self.contentHeightConstraint.tpbConstant = contentHeight; } } - (void)innerSwitchToPage:(NSInteger)page animated:(BOOL)animated { CGFloat x = self.scrollView.frame.size.width * page; [self.scrollView setContentOffset:CGPointMake(x, 0) animated:animated]; [self updateContentHeight]; [self reportPageChanged]; } #pragma mark - Public - (NSUInteger)currentPage { return self.pageControl.currentPage; } - (void)setContentHeight:(CGFloat)contentHeight { if (_contentHeight == contentHeight) { return; } _contentHeight = contentHeight; [self updateContentHeight]; } - (void)setLayoutMode:(TPBPageViewLayoutMode)layoutMode { if (_layoutMode == layoutMode) { return; } _layoutMode = layoutMode; [self makeConstraint]; } - (void)setShowPageControl:(BOOL)showPageControl { if (_showPageControl == showPageControl) { return; } _showPageControl = showPageControl; [self makeConstraint]; } - (void)updateContentViewArray:(NSArray<UIView *> *)contentViewArray { for (UIView *oldView in self.contentViewArray) { [oldView removeFromSuperview]; } for (UIView *newView in contentViewArray) { [self.scrollView addSubview:newView]; } self.contentViewArray = contentViewArray; self.pageControl.numberOfPages = contentViewArray.count; self.pageControl.currentPage = 0; self.scrollView.contentOffset = CGPointZero; [self setNeedsLayout]; [self updateContentHeight]; [self reportPageChanged]; } - (void)showPage:(NSInteger)page animated:(BOOL)animated { if (page == self.pageControl.currentPage) { return; } self.pageControl.currentPage = page; [self innerSwitchToPage:page animated:animated]; } @end
最新发布
12-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值