在接口构建器中应用自定义滑块

时间:2022-09-06 20:25:44

I want to create a custom slide, however I do not know how to apply it in IB.

我想创建一个自定义幻灯片,但是我不知道如何在IB中应用它。

.h

. h

@interface MainViewController : UIViewController <AVAudioPlayerDelegate> {
    UISlider                *customSlider;
}

@property (nonatomic, retain, readonly) UISlider *customSlider;

@end

.m

00

#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;
}

All I need to know is how I apply it in IB. As its an succesful build.

我只需要知道如何在IB中应用它,因为它是一个成功的构建。

Thanks

谢谢

1 个解决方案

#1


2  

You could make a custom class that is a subclass that inherits UISlider that does this setup in the init methods.

您可以创建一个自定义类,它是继承UISlider的子类,在init方法中进行这种设置。

From there in interface builder just drop a normal UISlider in and on the Identity tab of the Inspector window change the class to your custom class.

在接口构建器中,只需在检查器窗口的Identity选项卡中插入一个普通的UISlider,并将类更改为自定义类。

in your MySlider.h:

在你MySlider.h:


@interface MySlider : UISlider
{

// Anything relevant you might want to change

}
@end

And in the .m file:

在。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

#1


2  

You could make a custom class that is a subclass that inherits UISlider that does this setup in the init methods.

您可以创建一个自定义类,它是继承UISlider的子类,在init方法中进行这种设置。

From there in interface builder just drop a normal UISlider in and on the Identity tab of the Inspector window change the class to your custom class.

在接口构建器中,只需在检查器窗口的Identity选项卡中插入一个普通的UISlider,并将类更改为自定义类。

in your MySlider.h:

在你MySlider.h:


@interface MySlider : UISlider
{

// Anything relevant you might want to change

}
@end

And in the .m file:

在。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