如何在运行时自定义 iPhone UISlider 控件 - 在哪里放置颜色更新代码?

发布于 2024-08-21 21:29:47 字数 1856 浏览 6 评论 0原文

我已将 UISlider 控件添加到基于 iPhone 视图的应用程序,并连接了一个从滑块获取值的文本字段。为了适应应用程序中的用户界面颜色,我需要更改滑块的颜色。

通过一些谷歌搜索,我找到了 2 个执行此操作的示例(http://blog.hill-it.be/post/2009/03/23/Pimp-My-UISliderhttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2024-08-28 21:29:47

你不应该这样做。您有三个选择:

  1. 不让您的方法返回新的 UISlider 对象,而是让它修改您在 IB 中创建的现有 UISlider 实例。
  2. 编写自定义 UISlider 子类并将自定义代码放入其 awakeFromNib 方法中。使该类成为IB中滑块实例的类。
  3. 从 NIB 中完全删除滑块并按照您在方法中所做的那样以编程方式创建它。然后将其作为子视图添加到主视图中。

You shouldn't do it this way. You have three options:

  1. Instead of having your method return a new UISlider object, have it modify the existing UISlider instance you created in IB.
  2. Write a custom UISlider subclass and put your customization code in its awakeFromNib method. Make this class the class of the slider instance in IB.
  3. Remove the slider from the NIB completely and create it programmatically as you are doing in your method. Then add it as a subview to your mainView.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文