Autolayout(使用Masonry库)

在使用自动布局之前一直对自动布局的动画实现有怀疑。

有文章说不断更改布局带来的内存消耗很大。于是做了个测试动画的demo,发现完全木有问题啊

原生的约束写起来很麻烦,还好有Masonry和UIView-Autolayout这些库。

另外看了这位大神对使用Masonry介绍的文章,加上Masonry的demo,很快就会用了,多亏有这些大神们的分享精神。

#import "MASExampleUpdatingFrequentlyTest.h"
@interface MASExampleUpdatingFrequentlyTest()<UIScrollViewDelegate>
{
    UIScrollView *scrollview;
    UIView *runningScreen;
    UILabel *offsetLabel;
}

@end

@implementation MASExampleUpdatingFrequentlyTest
-(instancetype)init
{
    if (self = [super init]) {
        scrollview = [[UIScrollView alloc]init];
        scrollview.backgroundColor = [UIColor grayColor];
        scrollview.delegate = self;
        [self addSubview:scrollview];
        scrollview.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 1000);
        
        runningScreen = [UIView new];
        runningScreen.backgroundColor = [UIColor blackColor];
        [self addSubview:runningScreen];
        
        
        offsetLabel = [UILabel new];
        offsetLabel.textAlignment = NSTextAlignmentCenter;
        offsetLabel.text =@"running boy!";
        offsetLabel.preferredMaxLayoutWidth = 300;
        offsetLabel.backgroundColor = [UIColor orangeColor];
        [runningScreen addSubview:offsetLabel];
        
        [scrollview mas_makeConstraints:^(MASConstraintMaker *make){
            make.edges.equalTo(self);
        }];
        [runningScreen mas_makeConstraints:^(MASConstraintMaker *make){
            make.width.equalTo(scrollview);
            make.height.equalTo(50);
            make.top.equalTo(self);
            make.leading.equalTo(self);
        }];
        [offsetLabel mas_makeConstraints:^(MASConstraintMaker *make){
            make.center.equalTo(runningScreen);
        }];
    }
    return self;
}

-(void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    CGFloat offsetY = aScrollView.contentOffset.y;
    NSLog(@"%f",offsetY);
    [runningScreen mas_updateConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(offsetY);
    }];
    offsetLabel.text = [NSString stringWithFormat:@"You have run %.f ",offsetY];
    // tell constraints they need updating
    [runningScreen setNeedsUpdateConstraints];
    
    // update constraints now so we can animate the change
    [runningScreen updateConstraintsIfNeeded];
    [runningScreen updateConstraints];
}

思路主要是改变约束的top属性达到移动的效果。

其实这个库的demo列的情况已经很全了,都试着用一遍基本的应该没啥问题了



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值