【问题标题】:UISwitch: Making the touch area larger than the default touch areaUISwitch:使触摸区域大于默认触摸区域
【发布时间】:2013-02-01 21:35:20
【问题描述】:

我想让 UISwitch 周围的触摸区域每边大 10 点。查看相关帖子 (UIButton: Making the hit area larger than the default hit area) 中的一些建议,我尝试使用以下方法增加 UISwitch 周围的框架,但是它会导致整个 UISwitch 拉伸以填充新框架。

有没有更合理的方法可以做到这一点?

// Increase margin around switch based on width
const CGFloat desiredWidth = 260.0f;  // real width is 240
const CGFloat margin = 0.5f * (desiredWidth - self.beginSwitch.frame.size.width);

// Add margin on all four sides of the switch
CGRect newFrame = self.beginSwitch.frame;
newFrame.origin.x -= margin;
newFrame.origin.y -= margin;
newFrame.size.width  += 2.0f * margin;
newFrame.size.height += 2.0f * margin;

self.beginSwitch.frame = newFrame;

【问题讨论】:

  • 在上面放一个不可见的按钮,当它被击中时,设置实际开关的按钮状态
  • 也许作为最后的手段可行。这种方法的问题是用户无法像他们期望的那样滑动开关。
  • 好点,我想我从来没有真正滑动过开关,我总是点击它们大声笑
  • 是的,我也是。我特意让这个开关变宽了,让用户“滑动”开始他们的会话。他们也可以点击,但我希望保留幻灯片功能。
  • 所以放四个隐形按钮,每边一个 :-)(或者只有一个,在开关后面)

标签: ios ipad uiswitch


【解决方案1】:

我通过在图像(或在您的情况下为开关)前面放置一个不可见的按钮(您的尺寸较大)来完成这些壮举。这样做,您可以设置开关的状态,并基于开关当前状态的倒数。还可以在不可见按钮的操作代码中执行您的实际操作。

【讨论】: