在使用自动布局之前一直对自动布局的动画实现有怀疑。
有文章说不断更改布局带来的内存消耗很大。于是做了个测试动画的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列的情况已经很全了,都试着用一遍基本的应该没啥问题了