美文网首页
自定义Segment

自定义Segment

作者: 倪大头 | 来源:发表于2018-01-31 10:30 被阅读86次

自定义一个继承自UIView的类用来写Segment的UI和动效:

SegmentView.h

#import <UIKit/UIKit.h>

@interface SegmentView : UIView

- (void)setTitleViewWithArray:(NSArray *)titleArray;

- (void)beginMoveWithProgress:(CGFloat)progress;

@end

SegmentView.m

#import "SegmentView.h"

@interface SegmentView()

@property (nonatomic, strong)UIScrollView *myScrollView;

@end

@implementation SegmentView
{
    CGFloat AW;
    NSMutableArray *labelArray;//存放标题的数组
    UIView *lineView;//标题下划线
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.myScrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.myScrollView];
    }
    return self;
}

//Segment标题布局
- (void)setTitleViewWithArray:(NSArray *)titleArray {
    //存放标题宽度的数组
    NSMutableArray *textWArray = [NSMutableArray array];
    labelArray = [NSMutableArray array];
    
    //计算scrollView.contentSize.width
    AW = 0;
    
    for (NSString *str in titleArray) {
        CGFloat textW = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.width;
        [textWArray addObject:@(textW)];
        //计算所有标题宽度总和
        AW = textW + AW;
    }
    
    AW = AW + titleArray.count * 20 + 20;
    
    self.myScrollView.contentSize = CGSizeMake(AW, 0);
    
    for (int i = 0; i < textWArray.count; i++) {
        //创建标题label,间隔20距离
        UILabel *lastLabel;
        
        CGFloat startW;//按钮frame.x的位置

        if (i > 0) {
            lastLabel = labelArray[i - 1];
            startW = CGRectGetMaxX(lastLabel.frame) + 20;
        }else {
            startW = 20;
        }
   
        CGFloat W = [textWArray[i] floatValue];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(startW, 0, W, self.frame.size.height)];
        titleLabel.text = titleArray[i];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont systemFontOfSize:14];
        [self.myScrollView addSubview:titleLabel];
        [titleLabel sizeToFit];
        titleLabel.center = CGPointMake(titleLabel.center.x, 40/2);
        [labelArray addObject:titleLabel];
    }
    
    lineView = [[UIView alloc]initWithFrame:CGRectMake(20, 38, [textWArray.firstObject floatValue], 1)];
    lineView.backgroundColor = [UIColor blackColor];
    [self.myScrollView addSubview:lineView];
}

//标题下划线的动效
- (void)beginMoveWithProgress:(CGFloat)progress {
    CGFloat decimal = progress - (int)progress;
    UILabel *fromLabel;
    UILabel *nextLabel;
    if (progress == labelArray.count - 1) {//最后一页
        fromLabel = labelArray[(int)progress];
        nextLabel = labelArray[(int)progress];
    }else {
        fromLabel = labelArray[(int)progress];
        nextLabel = labelArray[(int)progress + 1];
    }
    
    CGRect newFrame = lineView.frame;
    
    if (decimal < 0.5) {//前半页
        newFrame.origin.x = CGRectGetMinX(fromLabel.frame);
        newFrame.size.width = fromLabel.frame.size.width + (20 + nextLabel.frame.size.width) * decimal * 2;
    }else {//后半页
        newFrame.origin.x = CGRectGetMinX(fromLabel.frame) + (20 + fromLabel.frame.size.width) * (decimal - 0.5) * 2;
        newFrame.size.width = nextLabel.frame.size.width + (20 + fromLabel.frame.size.width) * (1 - decimal) * 2;
    }
    
    lineView.frame = newFrame;
    
    //选中的标题移动到屏幕中间
    [self setSelectLabelCenter:fromLabel];
}

- (void)setSelectLabelCenter:(UILabel *)selectLabel {
    if (selectLabel.center.x < UI_SCREEN_WIDTH/2) {
        [self.myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else if ((selectLabel.center.x + UI_SCREEN_WIDTH/2) < self.myScrollView.contentSize.width) {
        [self.myScrollView setContentOffset:CGPointMake(selectLabel.center.x - UI_SCREEN_WIDTH/2, 0) animated:YES];
    }else {
        [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.contentSize.width - UI_SCREEN_WIDTH, 0) animated:YES];
    }
}

@end

创建Segment类:

- (void)createView {
    //创建SegmentView
    NSArray *titleArray = @[@"一",@"两个",@"三个字",@"四个字的",@"一",@"两个",@"三个字",@"四个字的"];
    segmentView = [[SegmentView alloc]initWithFrame:CGRectMake(0, 64, UI_SCREEN_WIDTH, 40)];
    segmentView.backgroundColor = [UIColor whiteColor];
    [segmentView setTitleViewWithArray:titleArray];
    [self.view addSubview:segmentView];
    
    //创建一个Scrollview作为主界面
    UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(segmentView.frame), UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - segmentView.frame.size.height - 64)];
    myScrollView.contentSize = CGSizeMake(UI_SCREEN_WIDTH * titleArray.count, 0);
    myScrollView.showsHorizontalScrollIndicator = NO;//隐藏滚轮
    myScrollView.pagingEnabled = YES;
    myScrollView.delegate = self;
    [self.view addSubview:myScrollView];
    
    for (int i = 0; i < titleArray.count; i++) {
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(i*UI_SCREEN_WIDTH, 0, UI_SCREEN_WIDTH, myScrollView.frame.size.height)];
        //随机背景色
        bgView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
        [myScrollView addSubview:bgView];
    }
}

在scrollViewDidScroll中调用SegmentView的类方法,实现标题下划线动效:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x < 0 || scrollView.contentOffset.x > scrollView.contentSize.width - scrollView.bounds.size.width) {
        return;
    }

    CGFloat progress = scrollView.contentOffset.x/UI_SCREEN_WIDTH;
    [segmentView beginMoveWithProgress:progress];
}

相关文章

  • 自定义Segment

    自定义一个继承自UIView的类用来写Segment的UI和动效: SegmentView.h SegmentVi...

  • 自定义segment

    如果你还在为系统的UISegmentedControl蹩脚的功能而耿耿于怀,如果你还在仅仅为了制作一个简单的标签而...

  • 自定义segment

    https://github.com/zhanjiarong/JRSegmentControl https://g...

  • Swift开发 自定义Segment

    引言 自定义封装了一个Segment,使用简单,下面是效果图。 代码部分 首先定义一个SegmentStyle,把...

  • iOS自定义控件-Segment

    首先献上本控件的项目地址 使用方法 1.导入 #import "LXSegmentScrollView.h" 2....

  • iOS自定义Segment控件

    在我们开发中经常会遇到这样的需求 如下图: 下面是我自定义这个控件的过程: 新建一个UIView 新建一个XIB文...

  • UISegmentControl

    常用方法 新增一个文字segment 新增一个图片segment 删除某个segment 删除所有segment ...

  • 自定义的action sheet(封装整理),自定义的segme

    最近的项目,要求actionsheet,segment样式自定义,,,没办法把以前无聊写的改了改,大家可以用用,都...

  • Okio之Segment

    官方解释 Segment Segment 是 buffer 的切割部分. 每个 buffer 中的 Segment...

  • 关于“TCP segment of a reassembled

    关于“TCP segment of a reassembled PDU” 标签:TCP segment of a ...

网友评论

      本文标题:自定义Segment

      本文链接:https://www.haomeiwen.com/subject/mmoczxtx.html