首頁 > 軟體

iOS實現多個垂直滑動條並列檢視

2022-03-21 13:00:08

本文範例為大家分享了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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com