UISlider,设置不同范围的图像 [英] UISlider, setting images for different ranges

查看:147
本文介绍了UISlider,设置不同范围的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UISlider,我基本上把它分成5段。

  0 <= x < .2 
.2< = x< .4
.4< = x< .6
.6< = x< .8
.8< = x< = 1

与另一个scrollView缩放,当我移动滑块,scrollView缩放,AND当我捏在scrollView上,滑块移动。



在viewDidLoad中初始化滑块时,我为UIControlStateNormal和UIControlStateSelected创建了不同的thumbImages,如下所示:

  if([self.navigatorSlider value]> = 0&& [self.navigatorSlider value] <.2){
[self.navigatorSlider setThumbImage:[UIImage imageNamed:@firstZoom.png] forState:UIControlStateNormal];
[self.navigatorSlider setThumbImage:[UIImage imageNamed:@firstZoom.png] forState:UIControlStateSelected];
}

我为上面提到的5个不同部分然而在IBAction方法我有滑块,我需要设置thumbImages再次做相同的检查。还在我的handleZoom方法,我需要setThumbImage在该方法太。我做错了什么?我觉得我应该能够设置这些值在某个地方,当滑块改变时,图像将会自动改变,无论它是来自处理滑块本身,还是放大scrollView和使用

解决方案

div>

看看UISlider提供的属性,你可以得到一个非常强烈的感觉,虽然类设计让你自定义它的外观,动态改变控件的外观,当用户调整它可能不是什么设计师在想。否则,他们会包括你建议的那种机制:一种方法将不同的图像与值的范围相关联。



如果你想要的第一件事简化你的方法是使用KVO来观察滑块。如果您可以在每次更新滑块的值时收到通知,则可以在一个位置更新缩略图。很抱歉, UISlider不完全支持KVO



接下来我会考虑创建自己的滑块控件。这不是火箭科学 - 你需要的是绘制控件,在上面绘制拇指,并允许拇指在一个维度上滑动。如果你把拇指作为控制的其余部分的子视图,这是一块蛋糕。然后你可以添加KVO支持,如果你想,或一路支持不同的缩略图的不同范围。



如果你不想这样做,你可能能够子类化UISlider以添加图像数组,范围数组和覆盖 -setValue:以使用它们。



最后,你现在所拥有的并不是那么糟糕。如果你在你的操作和 -handleZoom 方法中重复了代码,这不是那么好,但应该很容易将公共代码转换为适当的方法您可以从这两个地方进行呼叫。


I have a UISlider and I'm basically splitting it into 5 segments.

0 <= x < .2
.2 <= x < .4
.4 <= x < .6
.6 <= x < .8
.8 <= x <= 1

I'm trying to synchronize this with another scrollView that zooms that I have so when I move the slider, the scrollView zooms, AND when I pinch on the scrollView, the slider moves.

When I initialize my slider in the viewDidLoad, I created different thumbImages for the UIControlStateNormal and UIControlStateSelected like this:

if ([self.navigatorSlider value] >= 0 && [self.navigatorSlider value] < .2) {
    [self.navigatorSlider setThumbImage:[UIImage imageNamed:@"firstZoom.png"] forState:UIControlStateNormal];
    [self.navigatorSlider setThumbImage:[UIImage imageNamed:@"firstZoom.png"] forState:UIControlStateSelected];
}

I do this for each of the 5 diffrent sections I noted above. However in the IBAction method I have for the slider, I need to set the thumbImages again doing the same checks. Also in my handleZoom method I need to setThumbImage in that method too. Am I doing something wrong? I feel like I should be able to set these values somewhere and the images will be changed automatically when the slider changes, no matter if it comes from handling the slider itself, or zooming in on the scrollView and using the setValue method for the slider.

Thoughts?

解决方案

Looking at the properties that UISlider provides, you can get a pretty strong sense that while the class is designed to let you customize it's appearance, dynamically changing the appearance of the control as the user adjusts it probably wasn't what the designer had in mind. Otherwise, they'd have included the sort of mechanism you suggest: a means to associate different images with ranges of values.

The first thing that comes to mind if you want to simplify your approach is to use KVO to observe a slider. If you could get a notification every time the slider's value is updated, you could update the thumb image in one place. Unfortunately, it looks like UISlider doesn't fully support KVO.

The next thing I'd consider would be creating your own slider control. This isn't rocket science -- all you need is to draw the control, draw the thumb on top, and allow the thumb to slide in one dimension. If you make the thumb a subview of the rest of the control, that's a piece of cake. Then you can add KVO support if you want, or go all the way and support different thumb images for different ranges.

If you don't want to do that, you might be able to subclass UISlider to add your array of images, array of ranges, and an override of -setValue: to make use of them.

Finally, what you've got now isn't really so bad. If you've repeated the code in both your action and -handleZoom methods, that's not so good, but it should be simple to factor out the common code into an appropriate method that you can call from both places.

这篇关于UISlider,设置不同范围的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆