我有 3 个视图层次结构(弹出框、页面容器、页面视图),页面视图放置在页面容器内,页面容器放置在弹出框内。它们都属于 UIView 类。
我在页面容器上添加了滑动手势。当发生滑动时,页面视图将替换为另一个页面视图。我试图在刷卡后立即获得动画。它从向左滑动的当前状态开始,但是当我向右滑动时它会混乱,并且在所有滑动之后它继续保持混乱。由于某种原因,动画不一致。
下面是代码,我尝试将约束放在动画块中,它没有任何区别。
- (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];
}
}