gpt4 book ai didi

ios - 自动布局动画问题

转载 作者:行者123 更新时间:2023-12-01 17:16:17 24 4
gpt4 key购买 nike

我有 3 个 View 层次结构(弹出框、页面容器、页面 View ),页面 View 放置在页面容器内,页面容器放置在弹出框内。它们都属于 UIView 类。

我在页面容器上添加了滑动手势。当发生滑动时,页面 View 将替换为另一个页面 View 。我试图在刷卡后立即获得动画。它从向左滑动的当前状态开始,但是当我向右滑动时它会困惑,并且在所有滑动之后它继续保持困惑。由于某种原因,动画不一致。

下面是代码,我尝试将约束放在动画 block 中,它没有任何区别。

- (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];


}
}

最佳答案

自动布局的第一条规则是您不能触摸框架。

您不能更改自动布局层次结构中 View 的框架、边界或中心。

阅读您的代码有点令人困惑,所以我不能 100% 确定您实际上在制作什么动画。但是您需要做的是删除所有行,例如...

pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);

当您想在屏幕上移动某些东西时,您需要更新约束,以便新的约束意味着新的布局。

例如,如果您想为 View 的高度设置动画,那么您需要...

保留对自动布局约束的引用...
@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];

不过,我会努力使代码更具可读性。可能将东西移到不同的功能以减少代码复制等......

目前读起来很困惑。

关于ios - 自动布局动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com