English 中文(简体)
ios 设置 UISlider 的起始位置和递增
原标题:ios set start position and incrementation of a UISlider
  • 时间:2012-05-22 11:25:41
  •  标签:
  • ios
  • uislider

有谁知道如何设置 UISlider (理想处于中间) 的起始位置, 以及如何加量为 10 个而不是 10 个?

这是我到现在为止得到的:

// Setup custom slider images
UIImage *minImage = [UIImage imageNamed:@"ins_blueTrack.png"];
UIImage *maxImage = [UIImage imageNamed:@"ins_whiteTrack.png"];
UIImage *tumbImage= [UIImage imageNamed:@"slide.png"];

minImage=[minImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];
maxImage=[maxImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];

// Setup the  slider
[self.slider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[self.slider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[self.slider setThumbImage:tumbImage forState:UIControlStateNormal];

self.slider.minimumValue = 10;
self.slider.maximumValue = 180;
self.slider.continuous = YES;

int myVal = self.slider.value;
NSString *timeValue = [[NSString alloc] initWithFormat:@"%1d", myVal];

self.timeLabel.text = timeValue;

// Attach an action to sliding
[self.slider addTarget:self action:@selector(fxSliderAction) forControlEvents:UIControlEventValueChanged];
最佳回答

< 坚固 > 设置滑动器位置

根据你的代码,你可以有:

self.slider.value = (90);

注:当您从同一类中重新引用 IB Outlet 时,不存在真正需要 self

如果您想要真正动态的方式将 UISlider 设定到中间点,您只需将最大值除以2, 有效将其减半:

self.slider.value = (self.slider.maximumValue / 2);

10个以上/10个以上倍数的 " 强 " 升级

为此,我建议与你目前的情况稍有不同。 与其规定最低为10个,最高为180个,不如规定最低为1个,最高为18个,如何?

self.slider.minimumValue = 1;
self.slider.maximumValue = 18;

每次你取回滑动滑动的值, 只要乘以10 。 这样滑动滑动滑动滑动到18个不同的位置( 如你所愿), 你总是得到10个的倍数。

int trueSliderValue = self.slider.value * 10;
问题回答

如果您想要 UISlider 的起始位置( 理想在中间), 那么

slider.value = (slider.maximumValue / 2);

如果您想要以 10 s 而不是以 10 s 递增它, 那么您应该拥有如此大的最大值。 现在, 在滑动值中, 方法的修改会检查当前滑动值的地板和天花板, 然后设置为滑动值 。 value = new value

这应准确地将UISlider的中点定位为中点,而不论使用的最小值或最高值。

slider.minimumValue = -10.0
slider.maximumValue = 1000.0
let trueMidPoint = ((slider.maximumValue-0)-(0-slider.minimumValue))/2
slider.value = trueMidPoint




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签