美文网首页
iOS Segment滑动居中

iOS Segment滑动居中

作者: 邓布利多教授 | 来源:发表于2020-01-20 11:13 被阅读0次

    1、创建UIScrollView

    #pragma mark - 创建Segment
    -(UIView *)segmentView{
        
        if (!_segView) {
            _segView = [UIScrollView new];
            _segView.backgroundColor = LYColorWhite;
            _segView.showsHorizontalScrollIndicator = NO;
        }
        return _segView;
        
    }
    

    2、添加按钮

    for (int i = 0; i < titleArray.count; i ++) {
            
            LYLineButton *btnTitle = [LYLineButton new];
            [btnTitle setTitle:[NSString stringWithFormat:@"%@",titleArray[i]] forState:0];
            [btnTitle setTitleColor:[UIColor blackColor] forState:0];
            btnTitle.titleLabel.font = LYFONT_SISE_16;
            btnTitle.tag = i;
            [btnTitle addTarget:self action:@selector(buttonTitleSelect:) forControlEvents:UIControlEventTouchUpInside];
            [_segView addSubview:btnTitle];
            
            [btnTitle mas_makeConstraints:^(MASConstraintMaker *make) {
                
                if (lastButton) {
                    make.left.mas_equalTo(lastButton.mas_right).offset(20);
                }else{
                    make.left.mas_equalTo(10);
                }
                make.top.mas_equalTo(0);
                make.height.mas_equalTo(40);
                
            }];
            lastButton = btnTitle;
            
            
            if (i + 1 == titleArray.count) {
                
                [self layoutIfNeeded];
                CGFloat width = lastButton.frame.origin.x + lastButton.frame.size.width;
                _segView.contentSize = CGSizeMake(width + 10, LYHeight(lastButton));
                
            }
            
            //初始化的时候默认选中第一个按钮
            if (i == 0) {
                [btnTitle setTitleColor:LYColorDefualt forState:0];
                btnTitle.lineColor = LYColorDefualt;
            }
            
        }
    

    3、核心代码

    for (UIView *subView in self.segView.subviews) {
            if ([subView isKindOfClass:[LYLineButton class]]) {
                LYLineButton *subBtn = (LYLineButton *)subView;
                
                if (subBtn.tag == index) {
                    
                    //移动对应选择的当前视图显示在中央
                    CGRect centerRect = CGRectMake(subBtn.center.x - LYWidth(_segView)/2, 0, LYWidth(_segView), LYHeight(_segView));
                    [_segView scrollRectToVisible:centerRect animated:YES];
                    
                    //选中状态
                    if (subBtn.selected) {
                        subBtn.selected = NO;
                    }else{
                        subBtn.selected = YES;
                    }
                    [subBtn setTitleColor:LYColorDefualt forState:0];
                    subBtn.lineColor = LYColorDefualt;
                    
                    //标记通过ScrollView滑动时,不触发下面的动画
                    if (stamp) {
                        __weak typeof(self) wSelf = self;
                        [UIView animateWithDuration:0.2 animations:^{
                            wSelf.mainSView.contentOffset = CGPointMake(Screen_Width * subBtn.tag, 0);
                        }];
                    }
                    
                }else{
                    [subBtn setTitleColor:[UIColor blackColor] forState:0];
                    subBtn.lineColor = [UIColor clearColor];
                }
            }
        }
    

    4、最最核心代码

    CGRect centerRect = CGRectMake(subBtn.center.x - LYWidth(_segView)/2, 0, LYWidth(_segView), LYHeight(_segView));
    [_segView scrollRectToVisible:centerRect animated:YES];
    

    5、说明
    使用scrollRectToVisible:animated:方法的时候,contentSize的width和height都不能为0,否则不能滚动。

    相关文章

      网友评论

          本文标题:iOS Segment滑动居中

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