我想创建一个自定义幻灯片,但我不知道如何在IB中应用它。
·H
@interface MainViewController : UIViewController <AVAudioPlayerDelegate> {
UISlider *customSlider;
}
@property (nonatomic, retain, readonly) UISlider *customSlider;
@end
的.m
#define kSliderHeight 7.0
#define kViewTag 1
@implementation MainViewController
- (UISlider *)customSlider
{
if (customSlider == nil)
{
CGRect frame = CGRectMake(174, 12.0, 120.0, kSliderHeight);
customSlider = [[UISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
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;
[customSlider setAccessibilityLabel:NSLocalizedString(@"CustomSlider", @"")];
customSlider.tag = kViewTag;
}
return customSlider;
}
我需要知道的是我如何在IB中应用它。作为一个成功的构建。
由于
答案 0 :(得分:2)
您可以创建一个自定义类,它是继承UISlider的子类,在init方法中执行此设置。
在界面构建器中,只需在Inspector窗口的Identity选项卡中放置一个普通的UISlider,就可以将该类更改为您的自定义类。
在你的MySlider.h中:
@interface MySlider : UISlider
{
// Anything relevant you might want to change
}
@end
在.m文件中:
@implementation MySlider
- (id)init
{
self = [super init];
self.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];
[self setThumbImage: [UIImage imageNamed:@"slider_ball.png"] forState:UIControlStateNormal];
[self setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[self setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
self.minimumValue = 0.0;
self.maximumValue = 100.0;
self.continuous = YES;
self.value = 50.0;
[self setAccessibilityLabel:NSLocalizedString(@"CustomSlider", @"")];
return self;
}
@end