我正在尝试从viewController的底部顶部在中设置UIWebView的动画,并且我正在使用砖石(https://github.com/Masonry/Masonry)。
我最初创建的网络视图大小为0-(x,y,高度和宽度),然后我尝试为其设置动画,使网络视图在视图控制器的"顶部"设置动画。显示了网络视图,但它没有在适当的位置设置动画,只是立即显示。有经验的人能引导我朝着正确的方向前进吗?
这是我的按钮动作
-(void)didPressBtn{
self.infoView = [UIWebView new];
self.infoView.scalesPageToFit = YES;
self.infoView.backgroundColor = [UIColor whiteColor];
self.infoView.delegate = self;
[self.scrollView addSubview:self.infoView];
[self.infoView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(@(0));
}];
[self.scrollView layoutIfNeeded];
//FIXME: Hmmm it doesn't really animate!?
[UIView animateWithDuration:1 animations:^{
[self.scrollView setContentOffset:CGPointMake(0, 0)];
[self.infoView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
}];
[self.scrollView layoutIfNeeded];
} completion:^(BOOL finished) {
[self.infoView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:NSLocalizedString(@"INFORMATION_URL", )] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15]];
}];
}
我的视图DidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [UIScrollView new];
self.scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.scrollView];
[self.scrollView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
UIView *contentView = [UIView new];
[self.scrollView addSubview:contentView];
[contentView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
make.width.equalTo(self.scrollView);
}];
//Then adding buttons and such...
}
关于didPressButton
的一个快速观察。您正在使用mas_makeConstraints
将边设置为等于@0
,然后立即使用mas_makeConstraints
再次更改边,以填充滚动视图。这在第一个集合的顶部添加了相互矛盾的约束。
相反,您可以使用mas_remakeConstraints
来替换该视图上的现有约束。
至于动画,我使用的模式是首先更新约束(不在动画块内),然后对布局进行动画化:
[UIView animateWithDuration:1
animations:^{
[theParentView layoutIfNeeded];
}];
请参见如何设置约束更改的动画?