一步一步封裝分頁組件

最近在有個需求要求 類似網(wǎng)易新聞或者今日頭條上的UI效果


IMG_0753.png
Paste_Image.png
Paste_Image.png

JFSegmentDefunes.h

///標(biāo)題
@property (nonatomic,copy) NSString  *title;
///字體
@property (nonatomic,strong) UIFont  *font;
///間距
@property (nonatomic,assign) CGFloat  space;
///根據(jù)title計(jì)算frame的大小
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title;
///計(jì)算寬度
+ (CGFloat)calcuWidth:(NSString *)title;

JFSegmentDefunes.m

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title {
    self = [super initWithFrame:frame];
    if (self) {
        self.space = 8;
        self.title = title;
    }
    return self;
}

- (void)layoutSubviews {
    CGFloat buttonWidth = [self calcuWidth];
    self.frame = CGRectMake(self.x, self.y, buttonWidth + self.space, self.height);
    self.titleLabel.frame = CGRectMake(self.space / 2, 0, buttonWidth, self.height);
}

- (CGFloat)calcuWidth {
    if (self.title == nil) {
        return _MinWidth;
    }
    UIFont *font = self.font == nil ? _defaultFont : self.font;
    CGRect frame = [self.title boundingRectWithSize:CGSizeMake(_MaxWidth, self.height) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName: font} context:nil];
    CGFloat width = frame.size.width;
    return width > _MinWidth ? width : _MinWidth;
}

+ (CGFloat)calcuWidth:(NSString *)title {
    JFSegmentViewTitleItem *item = [[JFSegmentViewTitleItem alloc] initWithFrame:(CGRectZero) title:title];
    return [item calcuWidth] + item.space;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
        _titleLabel.textColor = _defaultTitleColor_Normal;
        _titleLabel.font = _defaultFont;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = self.title;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}
Paste_Image.png
Paste_Image.png
Paste_Image.png
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JFSegmentViewUnit * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JFSegmentViewUnit" forIndexPath:indexPath];
    UIViewController *vc = _viewControllers[indexPath.section];
    cell.view = vc.view;
    return cell;
}
    [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:(NSKeyValueObservingOptionNew) context:nil];

加一個KVO來監(jiān)聽collectionView的屬性的變化

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGPoint offset = self.collectionView.contentOffset;
    CGFloat pageFloat = offset.x / self.collectionView.bounds.size.width + 0.5;
    if (pageFloat < 0) {
        pageFloat = 0;
    }
    if (pageFloat > _viewControllers.count) {
        pageFloat = _viewControllers.count;
    }
    NSInteger page = (NSInteger)pageFloat;
    self.titleView.page = page;
}
JF.gif

大致的骨架基本上完后了 接下來優(yōu)化下UI

jf2.gif

那么怎么用它呢?如果用的不爽那搞這么多有個屁用

 SecondViewController *secondVC = [[SecondViewController alloc]init];
    secondVC.title = @"NBA";
    ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
    thirdVC.title = @"CBA";
    JFSegmentView *segmentView = [[JFSegmentView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height) titleHeight:44 viewControllers:@[secondVC,thirdVC]];
    [self.view addSubview:segmentView];

有幾個分頁那么就提供幾個控制器,設(shè)置titleView的高度,和當(dāng)前試圖的寬高
就是這么簡單
https://github.com/tubie/JFSegmentView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容