2015-11-06 66 views
0

我想實現這樣的事情:所以首先方法不叫這個自定義視圖

enter image description here

1,我已經實現了這樣的觀點:UnitSliderView:

enter image description here使用此代碼

#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,其使用先例視圖(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 - 問題:當我初始化一個MultiSlidersView在的viewController,數據源中的方法我在這同一個視圖控制器實現不叫,因此標籤不顯示,和滑塊值爲0

我覺得有一些我的代碼是不是在正確的地方,但我無法找到如何做不同。

回答

1

應定義創建類的DataSource之前調用init函數。您應該定義一個方法initWithFrame:(CGRect)frame andDataSource:(id <MultiSliderViewDataSource>) dataSource並使用它來初始化您的視圖。

+0

它解決了我的問題,謝謝。但我不確定這個解決方案是非常整潔的.. – Reveclair

0

我假設你想叫每當滑動值變化的行動。由於您使用代碼實現了所有內容,因此您可以使用代碼完成代碼。

[youSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];