iOS實現多個垂直滑動條并列視圖
更新時間:2022年03月21日 11:22:12 作者:hzgisme
這篇文章主要為大家詳細介紹了iOS實現多個垂直滑動條并列視圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS實現多個垂直滑動條并列視圖的具體代碼,供大家參考,具體內容如下
上一篇文章我們實現了一個垂直滑動條的類 (VerticalSlider),用來滿足垂直滑動的需求。那么這篇文章我們來把多個垂直滑動條放到一起,可以在一個視圖上并排多個垂直滑動條,也算是一個實際應用的場景。
需求:
- 同時展示多個垂直滑動條
- 每個滑動條高度和視圖高度相同,隨視圖高度自動變化
- 所有滑動條寬度相同,寬度為視圖寬度除以滑動條個數
- 根據提供的滑動條的值更新視圖
- 傳遞滑動條的索引和值
需求還是比較簡單的,自定義一個視圖 (UIView) 就可以實現:
VerticalSliderDimmingView.h
// // ?VerticalSliderDimmingView.h //? // // ?Created by huang zhengguo on 2019/8/30. // ?Copyright ? 2019 huang zhengguo. All rights reserved. // ? #import <UIKit/UIKit.h> ? NS_ASSUME_NONNULL_BEGIN ? typedef void (^SliderValueBlock) (NSInteger,float); ? @interface VerticalSliderDimmingView : UIView ? /** ?* 初始化手動調光界面 ?* ?* @param frame 大小 ?* @param sliderCount 滑動條個數 ?* @param channelColors 滑動條顏色 ?* @param sliderTitle 滑動條標題 ?* @param sliderValue 滑動條值 ?* ?*/ - (instancetype)initWithFrame:(CGRect)frame sliderCount:(NSInteger)sliderCount channelColors:(NSArray *)channelColors sliderTitles:(NSArray *)sliderTitle sliderValues:(NSArray *)sliderValue; ? /** ?* 更新滑動條 ?* ?* @param sliderValueArray 所有通道顏色值 ?* ?*/ - (void)updateSliderViewWith:(NSArray *)sliderValueArray; ? /** ?* 更新滑動條 ?* ?* @param colorPercentValueArray 所有通道顏色百分比 ?* ?*/ - (void)updateManualDimmingViewWithColorPercent:(NSArray *)colorPercentValueArray; ? // 滑動條滑動 @property(nonatomic, copy) SliderValueBlock colorDimmingBlock; ? // 滑動條結束滑動 @property(nonatomic, copy) SliderValueBlock colorDimmingEndBlock; ? @end ? NS_ASSUME_NONNULL_END
VerticalSliderDimmingView.m
//
// ?VerticalSliderDimmingView.m
//?
//
// ?Created by huang zhengguo on 2019/8/30.
// ?Copyright ? 2019. All rights reserved.
//
?
#import "VerticalSliderDimmingView.h"
#import "VerticalSlider.h"
?
@interface VerticalSliderDimmingView()
?
@property (strong, nonatomic) NSMutableArray *colorSliderArray;
?
@end
?
@implementation VerticalSliderDimmingView
?
- (NSMutableArray *)colorSliderArray {
? ? if (!_colorSliderArray) {
? ? ? ? _colorSliderArray = [NSMutableArray array];
? ? }
? ??
? ? return _colorSliderArray;
}
?
- (instancetype)initWithFrame:(CGRect)frame sliderCount:(NSInteger)sliderCount channelColors:(NSArray *)channelColors sliderTitles:(NSArray *)sliderTitle sliderValues:(NSArray *)sliderValue {
? ? if (self = [super initWithFrame:frame]) {
? ? ? ? self.translatesAutoresizingMaskIntoConstraints = NO;
? ? ? ??
? ? ? ? VerticalSlider *lastSlider = nil;
? ? ? ??
? ? ? ? [self.colorSliderArray removeAllObjects];
? ? ? ? for (int i=0; i<sliderCount; i++) {
? ? ? ? ? ? VerticalSlider *slider = [[VerticalSlider alloc] initWithFrame:CGRectZero title:[sliderTitle objectAtIndex:i] progressColor:[channelColors objectAtIndex:i] thumImage:@"control.png"];
? ? ? ? ? ??
? ? ? ? ? ? slider.minimumValue = MIN_LIGHT_VALUE;
? ? ? ? ? ? slider.maximumValue = MAX_LIGHT_VALUE;
? ? ? ? ? ? slider.translatesAutoresizingMaskIntoConstraints = NO;
? ? ? ? ? ? slider.tag = 20000 + i;
? ? ? ? ? ? slider.value = [sliderValue[i] integerValue] / 1000.0;
? ? ? ? ? ? slider.passValue = ^(float colorValue) {
? ? ? ? ? ? ? ? if (self.colorDimmingBlock) {
? ? ? ? ? ? ? ? ? ? self.colorDimmingBlock(slider.tag - 20000, colorValue * MAX_LIGHT_VALUE);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? ? ??
? ? ? ? ? ? slider.passEndValue = ^(float colorValue) {
? ? ? ? ? ? ? ? // 傳遞結束滑動時的顏色值
? ? ? ? ? ? ? ? if (self.colorDimmingEndBlock) {
? ? ? ? ? ? ? ? ? ? self.colorDimmingEndBlock(slider.tag - 20000, colorValue * MAX_LIGHT_VALUE);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? ? ??
? ? ? ? ? ? [self addSubview:slider];
? ? ? ? ? ??
? ? ? ? ? ? if (i == 0) {
? ? ? ? ? ? ? ? [self setSliderConstraintsWithItem:slider toItem:self toItem:self isFirst:YES isLast:NO];
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? [self setSliderConstraintsWithItem:slider toItem:self toItem:lastSlider isFirst:NO isLast:NO];
? ? ? ? ? ? }
?
? ? ? ? ? ? // 處理最后一個
? ? ? ? ? ? if (i == sliderCount - 1) {
? ? ? ? ? ? ? ? [self setSliderConstraintsWithItem:slider toItem:self toItem:lastSlider isFirst:NO isLast:YES];
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? lastSlider = slider;
? ? ? ? ? ??
? ? ? ? ? ? [self.colorSliderArray addObject:slider];
? ? ? ? }
? ? }
? ??
? ? return self;
}
?
- (void)sliderTouchUpInSideAction:(UISlider *)slider {
? ? // 傳遞d結束滑動時的顏色值
? ? if (self.colorDimmingEndBlock) {
? ? ? ? self.colorDimmingEndBlock(slider.tag - 20000, slider.value);
? ? }
}
?
#pragma mark --- 根據數據更新視圖
- (void)updateSliderViewWith:(NSArray *)sliderValueArray {
? ? // 刷新滑動條
? ? for (int i=0;i<self.colorSliderArray.count;i++) {
? ? ? ? VerticalSlider *slider = [self.colorSliderArray objectAtIndex:i];
? ? ? ? slider.value = [sliderValueArray[i] floatValue] / 1000.0;
? ? }
}
?
#pragma mark --- 根據百分比更新視圖
- (void)updateManualDimmingViewWithColorPercent:(NSArray *)colorPercentValueArray {
? ? for (int i=0; i<colorPercentValueArray.count; i++) {
? ? ? ? UISlider *slider = [self.colorSliderArray objectAtIndex:i];
? ? ? ? slider.value = (float)[[colorPercentValueArray objectAtIndex:i] floatValue] * 10;
? ? }
}
?
#pragma mark --- 添加滑動條約束
- (void)setSliderConstraintsWithItem:(nullable id)view1 toItem:(nullable id)view2 toItem:(nullable id)view3 isFirst:(BOOL)isFirst isLast:(BOOL)isLast {
? ? NSLayoutConstraint *sliderTopLayoutConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
? ? NSLayoutConstraint *sliderLeadingLayoutConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view3 attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
? ? NSLayoutConstraint *sliderBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
? ??
? ? if (!isFirst) {
? ? ? ? NSLayoutConstraint *sliderWithLayoutConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view3 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0];
? ? ? ??
? ? ? ? [self addConstraint:sliderWithLayoutConstraint];
? ? } else {
? ? ? ? sliderLeadingLayoutConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
? ? }
? ??
? ? // 最后一個
? ? if (isLast) {
? ? ? ? NSLayoutConstraint *sliderTrailingLayoutConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
? ? ? ??
? ? ? ? [self addConstraint:sliderTrailingLayoutConstraint];
? ? }
? ??
? ? [self addConstraints:@[sliderTopLayoutConstraint, sliderLeadingLayoutConstraint, sliderBottomLayoutConstraint]];
}
?
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
? ? // Drawing code
}
*/
?
@end以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IOS 開發(fā)自定義條形ProgressView的實例
這篇文章主要介紹了IOS 開發(fā)自定義條形ProgressView的實例的相關資料,希望開發(fā)自己的條形進度條的朋友可以參考下2016-10-10

