1.addtarget 的。部分使用事件没有直接的操作方式,需要进行调用。就要用addTarget。
- (void)setupCustomView {
self.customView= [[CHViewalloc] init];
self.customView.translatesAutoresizingMaskIntoConstraints=NO;
[self.viewaddSubview:self.customView];
NSDictionary *viewsDictionary =@{@"topLayoutGuide":self.topLayoutGuide,
@"customView":self.customView
};
[self.viewaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[topLayoutGuide][customView]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.viewaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[customView]|"
options:0
metrics:nil
views:viewsDictionary]];
[selfsetupSliders];
[selfupdateColors];
[selfsetupTargetActions];
}
- (void)setupSliders {
self.customView.trackWidthSlider.maximumValue=100.0;
self.customView.gaugeWidthSlider.maximumValue=100.0;
self.customView.valueSlider.value=self.customView.gauge.value;
self.customView.trackWidthSlider.value=self.customView.gauge.trackWidth;
self.customView.gaugeWidthSlider.value=self.customView.gauge.gaugeWidth;
self.customView.valueSliderLabel.text= [selfformattedStringForFloatValue:(self.customView.gauge.value*100)];
self.customView.trackWidthSliderLabel.text= [selfformattedStringForFloatValue:self.customView.gauge.trackWidth];
self.customView.gaugeWidthSliderLabel.text= [selfformattedStringForFloatValue:self.customView.gauge.gaugeWidth];
self.customView.valueSlider.continuous=NO;
self.customView.trackWidthSlider.continuous=NO;
self.customView.gaugeWidthSlider.continuous=NO;
}
- (void)updateColors {
self.customView.valueColorIndicatorView.backgroundColor=self.customView.gauge.textColor;
self.customView.trackColorIndicatorView.backgroundColor=self.customView.gauge.trackTintColor;
self.customView.gaugeColorIndicatorView.backgroundColor=self.customView.gauge.gaugeTintColor;
}
- (void)setupTargetActions {
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeState:)];
[self.customView.gaugeaddGestureRecognizer:tapGestureRecognizer];
[self.customView.gaugeStyleSwitchaddTarget:selfaction:@selector(changeGuageStyle:) forControlEvents:UIControlEventValueChanged];
[self.customView.valueSlideraddTarget:selfaction:@selector(valueSliderChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.customView.trackWidthSlideraddTarget:selfaction:@selector(trackWidthSliderChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.customView.gaugeWidthSlideraddTarget:selfaction:@selector(gaugeWidthSliderChangedValue:) forControlEvents:UIControlEventValueChanged];
tapGestureRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeValueColor:)];
[self.customView.valueColorIndicatorViewaddGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeTrackColor:)];
[self.customView.trackColorIndicatorViewaddGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeGaugeColor:)];
[self.customView.gaugeColorIndicatorViewaddGestureRecognizer:tapGestureRecognizer];
}
#PRagma mark - Action Methods
- (void)changeState:(UITapGestureRecognizer *)tapGestureRecognizer {
if(self.customView.gauge.state!= CHCircleGaugeViewStateNA) {
self.customView.gauge.state= CHCircleGaugeViewStateNA;
}else {
[self.customView.gaugesetValue:self.customView.gauge.valueanimated:YES];
}
}