我想实现这样的事情:
1-首先,我实现了这个视图:UnitSliderView:
使用此代码:
#import "UnitSliderView.h"
@implementation UnitSliderView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
//initialization
self.backgroundColor = [UIColor grayColor];
[self initViews];
[self initConstraints];
}
return self;
}
-(void)initViews
{
_leftLabel = [[UILabel alloc] init];
_leftLabel.textColor = [UIColor whiteColor];
//self.leftLabel.text = @"John";
[self addSubview:_leftLabel];
_rightLabel = [[UILabel alloc] init];
_rightLabel.textColor = [UIColor whiteColor];
//self.rightLabel.text = @"20%";
[self addSubview:self.rightLabel];
_slider = [[UISlider alloc] init];
_slider.value = 0.6;
[self addSubview:_slider];
}
-(void)initConstraints
{
self.rightLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.leftLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.slider.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{
@"leftLabel": self.leftLabel,
@"slider": self.slider,
@"rightLabel": self.rightLabel,
};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[leftLabel(==90)]-5-[slider(==120)]-[rightLabel(==50)]-|" options:0 metrics:nil views:views]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.leftLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.rightLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
@end
2-然后,我创建了这个名为MultiSliderView的视图,它使用了先前的View(UnitSliderView):
#import "MultiSliderView.h"
#import "UnitSliderView.h"
static const int kUnitsCount = 3;
@implementation MultiSliderView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
//initialization
self.backgroundColor = [UIColor clearColor];
[self initViews];
}
return self;
}
- (void)initViews {
float totalHeight = self.frame.size.height;
float spacingRatio = 0.1;
float heightPerUnit = totalHeight/(kUnitsCount+spacingRatio*(kUnitsCount));
for (int i = 0; i < kUnitsCount; i++)
{
double percentage = [self.dataSource valueForSliderAtIndex:i];
NSString *leftString = [self.dataSource stringForLeftLabelAtIndex:i];
NSString *rightString = [self.dataSource stringForRightLabelAtIndex:i];
UnitSliderView *unit = [[UnitSliderView alloc] initWithFrame:CGRectMake(0, i*heightPerUnit + (i)*heightPerUnit*spacingRatio, self.frame.size.width, heightPerUnit)];
unit.slider.value = percentage;
unit.leftLabel.text = leftString;
unit.rightLabel.text = rightString;
[self addSubview:unit];
}
}
@end
3 - 问题:当我在viewController中初始化MultiSlidersView时,我没有调用我在同一个视图控制器中实现的数据源方法,因此不显示标签和滑块值是0。
我认为我的某些代码不在正确的位置,但我无法以不同的方式找到它。
答案 0 :(得分:1)
在调用init函数之前,您应该在之前定义已创建的类<em>的dataSource。您应该定义方法initWithFrame:(CGRect)frame andDataSource:(id <MultiSliderViewDataSource> ) dataSource
并使用它来初始化您的视图。
答案 1 :(得分:0)
我假设您希望在幻灯片值更改时调用操作。由于您在代码中实现了所有内容,因此可以使用以下代码在代码中执
[youSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];