Skip to content

Commit

Permalink
refactor: 用不那么冗余的方法重写了对于Array的设置方法
Browse files Browse the repository at this point in the history
  • Loading branch information
XVXVXXX committed Dec 23, 2015
1 parent 5cbf5de commit 8b41093
Showing 1 changed file with 20 additions and 46 deletions.
66 changes: 20 additions & 46 deletions MasonryExample/Case2ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,53 +76,27 @@ - (void)initViews {
//分别设置每个imageView的宽高、左边、垂直中心约束,注意约束的对象
//每个View的左边约束和左边的View的右边相等=。=,有点绕口...

UIImageView *imageView1 = _imageViews[0];
MASConstraint *width = [self setView:imageView1 size:imageViewSize left:_containerView.mas_left centerY:_containerView.mas_centerY];
[_widthConstraints addObject:width];

UIImageView *imageView2 = _imageViews[1];
width = [self setView:imageView2 size:imageViewSize left:imageView1.mas_right centerY:_containerView.mas_centerY];
[_widthConstraints addObject:width];

UIImageView *imageView3 = _imageViews[2];
width = [self setView:imageView3 size:imageViewSize left:imageView2.mas_right centerY:_containerView.mas_centerY];
[_widthConstraints addObject:width];

UIImageView *imageView4 = _imageViews[3];
width = [self setView:imageView4 size:imageViewSize left:imageView3.mas_right centerY:_containerView.mas_centerY];
[_widthConstraints addObject:width];

//最后设置最右边的imageView的右边与父view的最有对齐
[imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_containerView.mas_right);
UIView __block *lastView = nil;
MASConstraint __block *widthConstraint = nil;
NSUInteger arrayCount = _imageViews.count;
[_imageViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
[view mas_makeConstraints:^(MASConstraintMaker *make) {
//宽高固定
widthConstraint = make.width.equalTo(@(imageViewSize.width));
make.height.equalTo(@(imageViewSize.height));
//左边约束
make.left.equalTo(lastView ? lastView.mas_right : view.superview.mas_left);
//垂直中心对齐
make.centerY.equalTo(view.superview.mas_centerY);
//设置最右边的imageView的右边与父view的最有对齐
if (idx == arrayCount - 1) {
make.right.equalTo(view.superview.mas_right);
}

[_widthConstraints addObject:widthConstraint];
lastView = view;
}];
}];
}

/**
* 设置view的宽高、左边约束,垂直中心约束
*
* @param view 要设置的view
* @param size CGSize
* @param left 左边对齐的约束
* @param centerY 垂直中心对齐的约束
*
* @return 返回宽约束,用于显示、隐藏单个view
*/
- (MASConstraint *)setView:(UIView *)view size:(CGSize)size left:(MASViewAttribute *)left centerY:(MASViewAttribute *)centerY {

__block MASConstraint *widthConstraint;

[view mas_makeConstraints:^(MASConstraintMaker *make) {
//宽高固定
widthConstraint = make.width.equalTo(@(size.width));
make.height.equalTo(@(size.height));
//左边约束
make.left.equalTo(left);
//垂直中心对齐
make.centerY.equalTo(centerY);
}];

return widthConstraint;
}

@end

0 comments on commit 8b41093

Please sign in to comment.