UIStackView 对UIView子视图进行管理 ,对原片进行缩放
首先,建立一些视图,将视图放在数组中:
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 4; i ++) {
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
[array addObject:view];
}
// 初始化方法,通过数组传入被管理的视图
self.stackView = [[UIStackView alloc] initWithArrangedSubviews:array];
[self.view addSubview:self.stackView];
self.stackView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 500);
self.stackView.axis = UILayoutConstraintAxisVertical; // 垂直方向
/*
typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {
UILayoutConstraintAxisHorizontal = 0, 水平布局
UILayoutConstraintAxisVertical = 1 竖直布局
};
*/
self.stackView.distribution = UIStackViewDistributionFillEqually; // 排列方式
/*
typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
UIStackViewDistributionFill = 0, // 充满,当只有一个控件时可以使用
UIStackViewDistributionFillEqually, // 平分充满,每个控件占据相同尺寸排列充满
UIStackViewDistributionFillProportionally, // 会优先按照约束的尺寸进行排列,如果没有充满,会拉伸最后一个排列的控件充满
UIStackViewDistributionEqualSpacing, // 等间距排列
UIStackViewDistributionEqualCentering, // 中心距离相等
} NS_ENUM_AVAILABLE_IOS(9_0);
*/
self.stackView.alignment = UIStackViewAlignmentFill; // 对齐模式
/*
typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
UIStackViewAlignmentFill, // 水平布局时为高度充满,竖直布局时为宽度充满
UIStackViewAlignmentLeading, // 前边对齐
UIStackViewAlignmentTop = UIStackViewAlignmentLeading, // 顶部对齐
UIStackViewAlignmentFirstBaseline, // 第一个控件文字的基线对齐 水平布局有效
UIStackViewAlignmentCenter, // 中心对齐
UIStackViewAlignmentTrailing, // 后边对齐
UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing, // 底部对齐
UIStackViewAlignmentLastBaseline, // 基线对齐,水平布局有效
} NS_ENUM_AVAILABLE_IOS(9_0);
*/
self.stackView.spacing = 5; // 设置最小间距
self.stackView.baselineRelativeArrangement = YES; // 设置布局是是否参照基线
self.stackView.layoutMarginsRelativeArrangement = NO; // 设置布局时是否以控件的LayoutMargins为标准,默认为NO,是以控件的bounds为标准
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(50, self.view.bounds.size.height - 50, 50, 50)];
[button2 addTarget:self action:@selector(removeView) forControlEvents:(UIControlEventTouchUpInside)];
[button2 setTitle:@"删除" forState:(UIControlStateNormal)];
button2.titleLabel.font = [UIFont systemFontOfSize:25];
[button2 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
[self.view addSubview:button2];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(150, self.view.bounds.size.height - 50, 50, 50)];
[button1 addTarget:self action:@selector(addView) forControlEvents:(UIControlEventTouchUpInside)];
[button1 setTitle:@"添加" forState:(UIControlStateNormal)];
button1.titleLabel.font = [UIFont systemFontOfSize:25];
[button1 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
[self.view addSubview:button1];
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(250, self.view.bounds.size.height - 50, 50, 50)];
[button3 addTarget:self action:@selector(insertView) forControlEvents:(UIControlEventTouchUpInside)];
[button3 setTitle:@"插入" forState:(UIControlStateNormal)];
button3.titleLabel.font = [UIFont systemFontOfSize:25];
[button3 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
[self.view addSubview:button3];
//响应事件
- (void)addView{
UIView *newView = [[UIView alloc] init];
newView.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
[self.stackView addArrangedSubview:newView];
// 在添加的时候会有动画效果
[UIView animateWithDuration:1 animations:^{
[self.stackView layoutIfNeeded];
}];
}
- (void)removeView{
UIView *view = [[self.stackView arrangedSubviews] lastObject]; // 获取被管理的所有视图中的最后一个
[self.stackView removeArrangedSubview:view]; // 移除一个view
[UIView animateWithDuration:1 animations:^{
[self.stackView layoutIfNeeded];
}];
}
- (void)insertView{
UIView *newView = [[UIView alloc] init];
newView.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
NSArray *array = [self.stackView arrangedSubviews];
if (array.count > 2) {
[self.stackView insertArrangedSubview:newView atIndex:2]; // 插入一个视图
[UIView animateWithDuration:1 animations:^{
[self.stackView layoutIfNeeded];
}];
}
}