Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单行选中的时候会有偏移的问题 #212

Open
CainLuo opened this issue Dec 12, 2020 · 7 comments
Open

单行选中的时候会有偏移的问题 #212

CainLuo opened this issue Dec 12, 2020 · 7 comments

Comments

@CainLuo
Copy link

CainLuo commented Dec 12, 2020

            let pickerView = BRStringPickerView()
            pickerView.pickerMode = .componentSingle
            pickerView.dataSourceArr = ["1", "2", "3"]
            pickerView.resultModelBlock = { result in
                
            }
            
            let style = BRPickerStyle()
            style.selectRowTextColor = .red
            style.separatorColor = .clear
            pickerView.pickerStyle = style
            
            pickerView.show()

上面是设置的代码, 设置在慢慢滑动的时候会出现偏移的问题, 如下图所示

Simulator Screen Shot - iPhone 12 mini - 2020-12-12 at 18 58 24

@agiapp
Copy link
Owner

agiapp commented Jan 21, 2021

这是系统滚轮组件的特性,单列或多列,选择行向未选择行滚动过渡时,都会有略微偏移

@Nemo01
Copy link

Nemo01 commented Aug 30, 2021

多列选择行的旁边两列选中的日期偏移太严重有办法处理没?

@agiapp
Copy link
Owner

agiapp commented Aug 30, 2021

这是UIPickerView组件默认样式,突出3D效果,所以会有偏移,暂时无法修改。一般内容字符串比较短的时候偏移会明显一点。

@Nemo01
Copy link

Nemo01 commented Aug 30, 2021

貌似用系统的UIDatePicker好像就没这个偏移问题,只有一点点基本没有

@agiapp
Copy link
Owner

agiapp commented Aug 30, 2021

UIPickerView都会有这个问题,UIDatePicker也封装了常用的时间类型

@agiapp
Copy link
Owner

agiapp commented Aug 30, 2021

针对使用UIPickerView组件,第1列和最后1列偏移较大问题,有如下临时解决方案。

BRPickerStyle.m文件 425 行下面添加如下代码:
image

添加代码

// 4.控制中间行label的文本偏移量
CGFloat offsetValue = 20; // 文本偏移的值
NSInteger componentCount = 3; // 选择器的列数
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // 当前选中的 label
    UILabel *selectLabel = (UILabel *)[pickerView viewForRow:row forComponent:component];
    if (selectLabel) {
        // 控制中间行label的文本偏移量
        if (component == 0) {
            CGRect rect = selectLabel.frame;
            rect.size.width = [UIScreen mainScreen].bounds.size.width / componentCount + offsetValue; // 第1列的偏移量,可根据实际效果进行调整
            selectLabel.frame = rect;
        } else if (component == 2) {
            CGRect rect = selectLabel.frame;
            rect.size.width = [UIScreen mainScreen].bounds.size.width / componentCount - offsetValue; // 第3列的偏移量,可根据实际效果进行调整
            selectLabel.frame = rect;
        }
    }
});

调整前的效果

image

调整后的效果

image

  • 以上是3列的情况,其它情况自行修改上面的offsetValuecomponentCount的值进行调整,也可以针对不同的列设置不同偏移量,以实际效果为准。

@Nemo01
Copy link

Nemo01 commented Aug 30, 2021

多谢,我试试看效果

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants