[英]CollisionBehaviour do not working with autolayout
我正在嘗試實現兩個視圖之間的沖突。
firstView
是帶有文本字段的視圖。 如果某個文本字段變為firstRecognizer,並且鍵盤框架與firstView
重疊。 它向上移動,如果firstView
與碰撞secondView
,應該將其推過。 但這不會發生。 相反, firstView
向上移動直到與secondView
碰撞,然后放回原處。
這是我使用的代碼:
- (void) keyboardChangeValueWithFrame:(CGRect)keyboardFrame isOpening:(BOOL)isOpening isClosing:(BOOL)isClosing {
CGFloat animTime = .3f;
if (isOpening && !isClosing) {
CGRect authFillerViewFrame = self.regFillerView.frame;
CGFloat rectDif = keyboardFrame.origin.y - authFillerViewFrame.origin.y - authFillerViewFrame.size.height;
if ([_animator.behaviors containsObject:_collision]) {
[_animator removeBehavior:_collision];
}
if (rectDif < 0) {
[self.view layoutIfNeeded];
self.regFillerViewHorizCenterConst.constant = rectDif;
[self.logoView layoutIfNeeded];
[UIView animateWithDuration:animTime animations:^{
_collision = [[UICollisionBehavior alloc] initWithItems:@[self.regFillerView, self.logoView]];
_collision.translatesReferenceBoundsIntoBoundary = NO;
_collision.collisionDelegate = self;
_collision.collisionMode = UICollisionBehaviorModeItems;
[_animator addBehavior:_collision];
_collision.action = ^{
};
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
//
}
}];
}
} else if (!isOpening && isClosing) {
[self.view layoutIfNeeded];
self.regFillerViewHorizCenterConst.constant = 0.f;
[UIView animateWithDuration:animTime animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
//
}];
}
}
有人可以告訴我我的錯誤嗎? 或者,也許有人知道更好的解決方案?
我找到了解決方案。 我的錯誤是在調用layoutIfNeeded
之前將碰撞行為添加到動畫師。 現在,我在添加碰撞行為之前調用layoutIfNeeded
,它運行良好。
[UIView animateWithDuration:animTime animations:^{
_collision = [[UICollisionBehavior alloc] initWithItems:@[self.regFillerView, self.logoView]];
[self.view layoutIfNeeded];
_collision.translatesReferenceBoundsIntoBoundary = NO;
_collision.collisionDelegate = self;
_collision.collisionMode = UICollisionBehaviorModeItems;
[_animator addBehavior:_collision];
_collision.action = ^{
};
} completion:^(BOOL finished) {
if (finished) {
//
}
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.