JLTrackLabelView

来源:互联网 发布:金正恩的出路 知乎 编辑:程序博客网 时间:2024/05/15 13:29


在其他地方看到下面所示的控件效果,就自己实现了一下,废话不多说,直接上代码:



在下面方法中创建视图

- (void)_creatSubviews {        NSArray *array = @[@"闲置广场",@"通知",@"个人中心",@"其他"];        //创建四个按钮    for (int i = 0; i < array.count; i++) {        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];        button.frame = CGRectMake(kScreenWidth/array.count * i, 200, kScreenWidth/array.count, 50);        button.backgroundColor = [UIColor clearColor];        [button setTitle:array[i] forState:UIControlStateNormal];        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        [self addSubview:button];        button.tag = i;        button.titleLabel.font = [UIFont systemFontOfSize:20];                if (i == 0) {            //按钮选中标识视图            _selectView = [[UIView alloc]initWithFrame:button.frame];            _selectView.backgroundColor = [UIColor orangeColor];            _selectView.layer.cornerRadius = 5.0;            [self addSubview:_selectView ];        }                [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    }        [self bringSubviewToFront:_selectView];    _selectView.clipsToBounds = YES;        //超出范围剪切    //创建字体颜色为白色的label的俯视图    _labelSuperView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];    [_selectView addSubview:_labelSuperView];        //创建文字颜色为白色的label    for (int i = 0; i < array.count; i++) {        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/array.count * i, 0, kScreenWidth/array.count, 50)];        label.text = array[i];        label.textColor = [UIColor whiteColor];        label.font = [UIFont systemFontOfSize:20];        label.textAlignment = NSTextAlignmentCenter;        [_labelSuperView addSubview:label];    }}

点击按钮时执行下面方法

- (void)buttonAction:(UIButton *)button {        CGFloat xPos = button.frame.size.width * (- button.tag);            [UIView animateWithDuration:0.5 animations:^{        _labelSuperView.frame = CGRectMake(xPos, 0, kScreenWidth, 50);  //设置_labelSuperView位置                _selectView.center = button.center;    }];    }




1 0
原创粉丝点击