[英]Autolayout Animation issue
我有3个视图层次结构(popover,页面容器,pageview),pageview放在pagecontainer内,pagecontainer放在popover内。 它们都是UIView类。
我在pagecontainer上添加了滑动手势。 发生滑动时,页面视图将替换为另一个页面视图。 我要在滑动后立即获取动画。 它从当前状态开始向左滑动,但是当我向右滑动时会混乱,并且在所有滑动之后仍然保持混乱状态。 由于某些原因,动画不一致。
下面是代码,我尝试将约束放在动画块中,这没有任何区别。
- (IBAction)swipeLeft:(id)sender {
if (_currentPage < [_pages count] - 1) {
UIView *currentView = _pages[_currentPage];
UIView *pageContainer = [currentView superview];
[currentView removeFromSuperview];
++_currentPage;
if (_pageControl)
_pageControl.currentPage = _currentPage;
UIView *newPage = _pages[_currentPage];
[pageContainer addSubview:newPage];
newPage.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
newPage.frame = CGRectIntegral(newPage.frame);
[pageContainer.superview layoutIfNeeded];
NSDictionary *pageviews = NSDictionaryOfVariableBindings(newPage);
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[newPage]-10.0-|" options:0 metrics:nil views:pageviews]];
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
[UIView animateWithDuration:5.0
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[pageContainer.superview layoutIfNeeded];
}
completion:^(BOOL finished){
}];
UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[self adjustPopoverOrientationWithCurrentOrientation:window];
}
}
和...
- (IBAction)swipeRight:(id)sender {
if (_currentPage > 0) {
UIView *currentView = _pages[_currentPage];
UIView *pageContainer = [currentView superview];
[currentView removeFromSuperview];
--_currentPage;
if (_pageControl)
_pageControl.currentPage = _currentPage;
UIView *newPage = _pages[_currentPage];
[pageContainer addSubview:newPage];
newPage.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
newPage.frame = CGRectIntegral(newPage.frame);
[pageContainer.superview layoutIfNeeded];
NSDictionary *pageviews = NSDictionaryOfVariableBindings(newPage);
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[newPage]-10.0-|" options:0 metrics:nil views:pageviews]];
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
[UIView animateWithDuration:5.0
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[pageContainer.superview layoutIfNeeded];
}
completion:^(BOOL finished){
}];
UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[self adjustPopoverOrientationWithCurrentOrientation:window];
}
}
自动版式的第一个规则是您不能触摸框架。
您无法更改自动版式层次结构中视图的框架,边界或中心。
阅读您的代码有点令人困惑,因此我不能100%确定您实际上在制作动画。 但是,您需要做的是删除所有行,例如...
pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
当您想在屏幕上移动某些内容时,您需要更新约束,以便新约束暗示新布局。
例如,如果要设置视图高度的动画,则需要...
保留对自动布局约束的引用...
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
要么...
// if you're creating the constraint in Interface Builder then CTRL drag it like any other outlet.
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *heightConstraint;
然后更改约束的constant
属性。
// animate the height to 150.
heightConstraint.constant = 150;
并为布局更改设置动画...
[UIView animateWithDuration:5.0
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[pageContainer.superview layoutIfNeeded];
}
completion:^(BOOL finished){
}];
一旦知道需要更改哪些约束,就可以确定如何最好地构建约束,以便对其进行动画处理。
编辑
您还需要确保关闭自动调整遮罩大小的转换...
[someView setTranslatesAutoresizingMaskIntoConstraints:NO];
我将努力使代码更具可读性。 可能将内容移至不同的功能,以减少代码复制等。
目前令人困惑的阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.