1.Masonry是基于AutoLayout的高度封装
要是你直接用官方的AutoLayout来布局的话,看看下面的代码感受一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| UIView *view = UIView.new; view.backgroundColor = [UIColor purpleColor]; view.translatesAutoresizingMaskIntoConstraints = NO;//键值将Autoresizing转换为Constraints [self addSubview:view]; NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150]; [view addConstraint:width]; NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150]; [view addConstraint:height]; NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100]; [view.superview addConstraint:left]; NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]; [view.superview addConstraint:top];
|
2.所以还是乖乖的用Masonry吧 :)
创建一个UIScrollView以便测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| UIScrollView *sc = [[UIScrollView alloc]init]; sc.backgroundColor = [UIColor orangeColor]; // sc.bounces = NO;//禁止回弹效果 sc.delegate = self; [self addSubview:sc]; [sc mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.mas_equalTo(0); make.width.equalTo(self); make.height.mas_equalTo(200); }]; UIView *lastView;; for (int i = 0; i < [self randomColor].count; i ++) { UIView *view = [[UIView alloc]init]; view.backgroundColor = [self randomColor][i]; [sc addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.mas_equalTo(0); make.width.height.equalTo(sc); // if (lastView) { // make.left.mas_equalTo(lastView.mas_right).offset(0); // }else{ // make.left.mas_equalTo(sc).offset(0); // } make.left.mas_equalTo(lastView ? lastView.mas_right : sc).offset(0); }]; lastView = view; } [lastView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(sc).offset(0); }];
|
3.遵守其代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| - (NSArray *)randomColor{ NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor grayColor], nil]; // int number = arc4random() % colors.count; // return colors[number]; return colors; }
//分页效果 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if (!decelerate) { [self customPage:scrollView]; } }
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self customPage:scrollView]; }
- (void)customPage:(UIScrollView *)sc{ CGFloat contentOffset = sc.contentOffset.x;
NSInteger index = (contentOffset + sc.frame.size.width) / self.frame.size.width - 0.5; //-0.5的目的就是为了当刚好没有划到一半时,自动划过去
[sc setContentOffset:CGPointMake(index * self.frame.size.width, 0) animated:YES]; }
|