使用库实现自定义UISwitch - 我做错了什么导致了大小问题?

时间:2022-01-31 03:12:41

I'm using BMXSwitch to implement a custom UISlider, hopefully in the end looking something like this:

我正在使用BMXSwitch来实现自定义UISlider,希望最终看起来像这样:

使用库实现自定义UISwitch  - 我做错了什么导致了大小问题?

Where the animation is pretty much identical to the default UISwitch, where the knob moves, pulling the whole view over as well, and resting on the other side with the other option visible.

动画与默认UISwitch完全相同,旋钮移动,同时拉动整个视图,并在另一侧放置,另一个选项可见。

However, using that library (and messing around with the example included), the closest I can get to that is the following:

但是,使用该库(以及包含的示例),我可以得到的最接近的是:

使用库实现自定义UISwitch  - 我做错了什么导致了大小问题?

Here's a download link to my project if it would help.

这是我项目的下载链接,如果有帮助的话。

Here's the code I customized to do that:

这是我为此定制的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_switch1 setCanvasImage: [UIImage imageNamed: @"canvas-custom"]];
    [_switch1 setMaskImage: [UIImage imageNamed: @"mask-custom"]];

    [_switch1 setKnobImage: [UIImage imageNamed: @"slider-knob-custom"] forState: UIControlStateNormal];
    [_switch1 setKnobImage: [UIImage imageNamed: @"slider-knob-custom"] forState: UIControlStateHighlighted];
    [_switch1 setKnobImage: [UIImage imageNamed: @"slider-knob-custom"] forState: UIControlStateDisabled];

    [_switch1 setContentImage: [UIImage imageNamed: @"slider-background-custom"] forState: UIControlStateNormal];
    [_switch1 setContentImage: [UIImage imageNamed: @"slider-background-custom"] forState: UIControlStateDisabled];

    [self valueChanged: nil];
//    _switch1.on = YES;
}

I don't know what I'm doing wrong. I have a feeling it's just with image sizes (the images are included with that download link), so if you can just suggest to me the sizing I'd need, or whatever I need to do to fix this, I'd really appreciate the help, it's stumping me greatly.

我不知道我做错了什么。我觉得这只是图像尺寸(图像包含在下载链接中),所以如果你能告诉我我需要的尺寸,或者我需要做些什么来解决这个问题,我真的很感激帮助,这让我非常难过。

1 个解决方案

#1


1  

Your problem is inside the .xib. You have defined a BMXSwitch with a large frame. The frame of the switch must be equal to the size of your button, no more.

你的问题在.xib中。您已经定义了一个具有大框架的BMXSwitch。开关的框架必须等于按钮的大小,不再多了。

使用库实现自定义UISwitch  - 我做错了什么导致了大小问题?

#1


1  

Your problem is inside the .xib. You have defined a BMXSwitch with a large frame. The frame of the switch must be equal to the size of your button, no more.

你的问题在.xib中。您已经定义了一个具有大框架的BMXSwitch。开关的框架必须等于按钮的大小,不再多了。

使用库实现自定义UISwitch  - 我做错了什么导致了大小问题?