如何在运行时自定义 iPhone UISlider 控件 - 在哪里放置颜色更新代码?
我已将 UISlider 控件添加到基于 iPhone 视图的应用程序,并连接了一个从滑块获取值的文本字段。为了适应应用程序中的用户界面颜色,我需要更改滑块的颜色。
通过一些谷歌搜索,我找到了 2 个执行此操作的示例(http://blog.hill-it.be/post/2009/03/23/Pimp-My-UISlider 和 http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html)但我被困住了,因为我不确定我应该从哪里调用代码。
我有一种方法应该返回自定义 UISlider 对象,但是如何覆盖我通过位于 sliderViewController.m 和 sliderViewController.h 中的界面生成器创建的对象?
- (UISlider *)createCustomSlider
{
CGRect frame = CGRectMake(174, 12.0, 120.0, 7.0);
UISlider customSlider = [[UISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
customSlider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:@"orangeslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:@"yellowslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[customSlider setThumbImage: [UIImage imageNamed:@"slider_ball.png"] forState:UIControlStateNormal];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 100.0;
customSlider.continuous = YES;
customSlider.value = 50.0;
return customSlider;
}
使用 .NET,我通常会由 WinForms 编辑器生成一些代码,然后我可以通过创建新对象并分配给现有引用来覆盖这些代码。
I have added a UISlider control to an iPhone View-based application and have wired up a text field that gets the values from the slider. To fit with the user interface colors in the application I need to change the color of the slider.
With a little googling I was able to find 2 samples which do this (http://blog.hill-it.be/post/2009/03/23/Pimp-My-UISlider and http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html) but I'm stuck because I'm not sure where I should be calling the code from.
I have a method which should return a custom UISlider object, but how do I override the one that I created through interface builder that lives in my sliderViewController.m and sliderViewController.h?
- (UISlider *)createCustomSlider
{
CGRect frame = CGRectMake(174, 12.0, 120.0, 7.0);
UISlider customSlider = [[UISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
customSlider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:@"orangeslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:@"yellowslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[customSlider setThumbImage: [UIImage imageNamed:@"slider_ball.png"] forState:UIControlStateNormal];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 100.0;
customSlider.continuous = YES;
customSlider.value = 50.0;
return customSlider;
}
With .NET I'd normally have some code generated by the WinForms editor which I would then be able to override by creating a new object and assigning to the existing reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不应该这样做。您有三个选择:
awakeFromNib
方法中。使该类成为IB中滑块实例的类。You shouldn't do it this way. You have three options:
awakeFromNib
method. Make this class the class of the slider instance in IB.