使用[UIView transitionFromView:toView:...]
和佈局約束的正確方法是什麼?UIView transitionFromView:toView:和佈局約束
我想在superview約束toView中安裝約束。我不能在轉換調用之前做到這一點,因爲toView還沒有超級視圖。 (在調用之後但在運行循環前進)。等待直到完成塊來安裝它意味着該視圖將具有動畫而沒有約束。
使用[UIView transitionFromView:toView:...]
和佈局約束的正確方法是什麼?UIView transitionFromView:toView:和佈局約束
我想在superview約束toView中安裝約束。我不能在轉換調用之前做到這一點,因爲toView還沒有超級視圖。 (在調用之後但在運行循環前進)。等待直到完成塊來安裝它意味着該視圖將具有動畫而沒有約束。
我會使用UIViewAnimationOptionShowHideTransitionViews選項,該選項允許toView和fromView在轉換之前處於視圖層次結構中,但顯示一個並隱藏另一個。
設置爲隱藏視圖,將其添加到超級視圖,並在轉換前安裝約束。然後,您可以刪除完成塊中的舊視圖。事情是這樣的:
[toView setHidden: YES];
[containerView addSubview: toView];
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"|[toView]|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(containerView, toView)]];
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[toView]|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(containerView, toView)]];
[UIView transitionFromView: fromView toView: toView duration: 1.0 options: UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {
[fromView removeFromSuperview];
}];
添加過渡調用後的約束(但運行循環之前的進展)工作原理:我之前的那個測試無效:-(抱歉噪聲
NSArray *priorConstraints = _constraints;
[UIView transitionFromView:priorView
toView:newView
...
completion:^(BOOL finished) {
[_containerView removeConstraints:priorConstraints];
....
}];
_constraints = [self constrainSubview:newView toBeCongruentWithSuperview:_containerView];
- (NSArray/*[NSLayoutConstraint]*/ *)constrainSubview:(UIView *)subview
toBeCongruentWithSuperview:(UIView *)superview {
subview.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(subview);
NSArray *constraints = [NSLayoutConstraint
constraintsWithVisualFormat:@"H:-(0)-[subview]-(0)-"
options:0
metrics:nil
views:viewsDictionary];
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint
constraintsWithVisualFormat:@"V:-(0)-[subview]-(0)-"
options:0
metrics:nil
views:viewsDictionary]];
[superview addConstraints:constraints];
return constraints;
}
。從我看到的情況來看,即使使用show/hide選項,view也會自動從superview中移除。 – Andy
看看調用後的視圖層次結構,fromView不會從superview中移除(至少在iOS 6和iOS 7上) – honus
爲了使用帶有AutoLayout約束的翻轉動畫,我們必須設置'UIViewAnimationOptionShowHideTransitionViews' –