我有3个视图层次结构(popover,page container,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];
}
}
答案 0 :(得分:14)
自动布局的第一条规则是您无法触摸画面。
您无法更改自动布局层次结构中视图的边框,边界或中心。
阅读你的代码有点令人困惑,所以我不能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];
我努力使代码更具可读性。可能会将东西移到不同的函数以减少代码复制等...
此刻阅读令人困惑。