4

我的 UISegmentedControl 分隔线遇到了一个奇怪的错误。当我将手指拖开时,该段会丢失其突出显示状态图像,但分隔线会保留它。我需要拖动大约 100px 才能发生这种情况。

向下,目标:

命中目标

向下,手指被拖离目标:

偏离目标

假设 0 表示正常状态,1 表示选中状态,2 表示高亮状态;我有所有正确的分隔符图像,并为每种情况设置它们,即:

00, 01, 02
10, 11, 12
20, 21, 22

其他一切正常,如果我点击并按住目标没有问题。我不希望用户会点击和拖动,但我宁愿解决这个问题。有谁之前经历过这个吗?

4

3 回答 3

3

无需为“11”和“22”状态提供分隔符图像,因为从逻辑上讲,您不能有 2 个相邻的选定段或 2 个相邻的突出显示段。好吧,只有同时点击相邻的部分才能突出显示它们,但不太可能。

因此,您应该有以下分隔线图像:

00, 01, 02  
10, --, 12  
20, 21, --

此外,您还应该为所有 3 个不同状态(正常、选定和突出显示)提供 3 个背景图像。这些背景图像的原因以及如何创建它们我已经在这里解释了(在那个解释中,我只使用了 2 个背景图像用于正常和选定状态,但如果你想使用突出显示状态,那么你需要添加第三个背景图像)。

这是显示这些图像如何在 UISegmentedControl 中使用的图表: 在此处输入图像描述

然后使用以下外观方法设置它们:

// Set background images
[segmentedControl setBackgroundImage:background_image_normal
                            forState:UIControlStateNormal
                          barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:background_image_selected
                            forState:UIControlStateSelected
                          barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:background_image_highlighted
                            forState:UIControlStateHighlighted
                          barMetrics:UIBarMetricsDefault];

// Set divider images 0=Normal 1=Selected 2=Highlighted
[segmentedControl setDividerImage:divider_image_00
              forLeftSegmentState:UIControlStateNormal
                rightSegmentState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_01
              forLeftSegmentState:UIControlStateNormal
                rightSegmentState:UIControlStateSelected
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_10
              forLeftSegmentState:UIControlStateSelected
                rightSegmentState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_02
              forLeftSegmentState:UIControlStateNormal
                rightSegmentState:UIControlStateHighlighted
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_20
              forLeftSegmentState:UIControlStateHighlighted
                rightSegmentState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_12
              forLeftSegmentState:UIControlStateSelected
                rightSegmentState:UIControlStateHighlighted
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_21
              forLeftSegmentState:UIControlStateHighlighted
                rightSegmentState:UIControlStateSelected
                       barMetrics:UIBarMetricsDefault]
于 2013-03-21T13:04:29.330 回答
0

一种可能的解决方案是手动继承 UISegmentedControl,并覆盖它以确保在调用 UIResponder 事件时正确“清理”分隔符状态。可能是该段预计 touchesMoved 事件将在与初始 touchesBegan 相同的边界内发生。

于 2013-03-07T11:30:17.043 回答
0

不要忘记 UIControlStateSelected | UIControlStateHighlighted

// Set background images
UIImage *normalBackgroundImage = [UIImage imageNamed:@"segmented-background-unselected"];
UIImage *selectedBackgroundImage = [UIImage imageNamed:@"segmented-background-selected"];
[self setBackgroundImage:normalBackgroundImage
                forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
                forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:normalBackgroundImage
                forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
                forState:UIControlStateSelected | UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

// Set divider images
UIImage *buttonImageLeftSelected = [UIImage imageNamed:@"segmented-left-selected"];
UIImage *buttonImageNoneSelected = [UIImage imageNamed:@"segmented-none-selected"];
UIImage *buttonImageRightSelected = [UIImage imageNamed:@"segmented-right-selected"];

[self   setDividerImage:buttonImageNoneSelected
    forLeftSegmentState:UIControlStateNormal
      rightSegmentState:UIControlStateNormal
             barMetrics:UIBarMetricsDefault];

[self   setDividerImage:buttonImageLeftSelected
    forLeftSegmentState:UIControlStateSelected
      rightSegmentState:UIControlStateNormal
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageLeftSelected
    forLeftSegmentState:UIControlStateSelected | UIControlStateHighlighted
      rightSegmentState:UIControlStateNormal
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageLeftSelected
    forLeftSegmentState:UIControlStateSelected
      rightSegmentState:UIControlStateHighlighted
             barMetrics:UIBarMetricsDefault];

[self   setDividerImage:buttonImageRightSelected
    forLeftSegmentState:UIControlStateHighlighted
      rightSegmentState:UIControlStateSelected
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageRightSelected
    forLeftSegmentState:UIControlStateNormal
      rightSegmentState:UIControlStateSelected | UIControlStateHighlighted
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageRightSelected
    forLeftSegmentState:UIControlStateNormal
      rightSegmentState:UIControlStateSelected
             barMetrics:UIBarMetricsDefault];
于 2015-02-09T13:28:56.943 回答