iOS ScrollableStackView

  • 内容
  • 评论
  • 相关

超出视图自动滑动组件

.h
//
//  ScrollableStackView.h
//  UniversalApp
//
//  Created by voidcat on 2023/11/18.
//  Copyright © 2023 voidcat. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CYUIScrollView.h"
NS_ASSUME_NONNULL_BEGIN

@interface ScrollableStackView : UIView

// MARK: - Properties

@property (nonatomic, strong) CYUIScrollView *scrollView;
@property (nonatomic, strong) UIStackView *stackView;

// MARK: - Life Cycle

- (void)didMoveToSuperview;

// MARK: - Actions

- (void)addView:(UIView *)view;
- (void)insertView:(UIView *)view atIndex:(NSInteger)index;
- (void)removeView:(UIView *)view;

// MARK: - Configuration

@property (nonatomic) CGFloat spacing;
@property (nonatomic) UIStackViewAlignment alignment;
@property (nonatomic) UIStackViewDistribution distribution;

@property (nonatomic) BOOL showsVerticalScrollIndicator;
@property (nonatomic) BOOL bounces;
@property(nonatomic, assign) BOOL didSetupConstraints;
@property (nonatomic) UILayoutConstraintAxis direction; // 添加direction属性

@end

NS_ASSUME_NONNULL_END

.m
#import <UIKit/UIKit.h>
#import "ScrollableStackView.h"

@interface ScrollableStackView()

@end


@implementation ScrollableStackView

// MARK: - Properties

- (CYUIScrollView *)scrollView {
  if (!_scrollView) {
    _scrollView = [[CYUIScrollView alloc] initWithFrame:CGRectZero];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    _scrollView.layoutMargins = UIEdgeInsetsZero;
  }
  return _scrollView;
}

- (UIStackView *)stackView {
  if (!_stackView) {
    _stackView = [[UIStackView alloc] initWithFrame:CGRectZero];
    _stackView.translatesAutoresizingMaskIntoConstraints = NO;
    _stackView.axis = self.direction;
    _stackView.alignment = UIStackViewAlignmentFill;
    _stackView.distribution = UIStackViewDistributionFill;
  }
  return _stackView;
}

// MARK: - Life Cycle

- (void)didMoveToSuperview {
  [super didMoveToSuperview];
  
  self.translatesAutoresizingMaskIntoConstraints = NO;
  self.clipsToBounds = YES;
  
  [self addSubview:self.scrollView];
  [self.scrollView addSubview:self.stackView];
  
  [self setNeedsUpdateConstraints];
}

- (void)updateConstraints {

  [super updateConstraints];

  if (!self.didSetupConstraints) {

      [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
      }];
      
      if (self.direction == UILayoutConstraintAxisHorizontal) {
        // 水平方向
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.equalTo(self.scrollView);
          make.height.equalTo(self.scrollView);
        }];

      } else {
        // 垂直方向(默认)

          
          [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.scrollView);
            make.width.equalTo(self.scrollView);
          }];
      }
      
    
    self.didSetupConstraints = YES;
  }

}

// MARK: - Actions

- (void)addView:(UIView *)view {
  [self.stackView addArrangedSubview:view];
}

- (void)insertView:(UIView *)view atIndex:(NSInteger)index {
  [self.stackView insertArrangedSubview:view atIndex:index];
}

- (void)removeView:(UIView *)view {
  [self.stackView removeArrangedSubview:view];
}

// MARK: - Configuration

- (CGFloat)spacing {
  return self.stackView.spacing;
}

- (void)setSpacing:(CGFloat)spacing {
  self.stackView.spacing = spacing;
}

- (UIStackViewAlignment)alignment {
  return self.stackView.alignment;
}

- (void)setAlignment:(UIStackViewAlignment)alignment {
  self.stackView.alignment = alignment;
}

- (UIStackViewDistribution)distribution {
  return self.stackView.distribution;
}

- (void)setDistribution:(UIStackViewDistribution)distribution {
  self.stackView.distribution = distribution;
}

- (BOOL)showsVerticalScrollIndicator {
  return self.scrollView.showsVerticalScrollIndicator;
}

- (void)setShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator {
  self.scrollView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
}

- (BOOL)bounces {
  return self.scrollView.bounces;
}

- (void)setBounces:(BOOL)bounces {
  self.scrollView.bounces = bounces;
}

@end

//示例
    self.stackView = [[ScrollableStackView alloc] init];
    
    self.stackView.direction = UILayoutConstraintAxisHorizontal;
    [self.stBgView addSubview:self.stackView];
    self.stackView.backgroundColor = KRedColor;
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.stBgView);
    }];
    
    for (int i = 0; i < 10; i++) {
        UIView *view = [[UIView alloc] init];
        
        view.backgroundColor = [UIColor qmui_randomColor];
        [self.stackView addView:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(50, 50));
        }];
    }

评论

0条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注