[英]iOS UIView properties don't animate with CABasicAnimation
當人們再次看到這個問題彈出窗口時,我可以想象出很多的嘆息。 但是,我在這里,文檔和谷歌都閱讀了很多信息,但仍然沒有找到解決方案。 所以這里什么都沒有。
我正在嘗試重新創建Facebook登錄屏幕,其中間距和位置使用鍵盤設置動畫,以允許用戶仍然可以看到所有輸入字段和登錄按鈕。
這在我使用kCAFillModeForwards
並將removedOnCompletion
設置為NO
。 但是,正如在SO上的另一個帖子中所說,這似乎只是在視覺上改變了屬性,並且實際的敲擊位置沒有改變。 因此,當用戶似乎點擊輸入字段時,iOS會將其解釋為在背景上點擊。
所以我嘗試設置新的位置和大小但是當我這樣做動畫不播放時,它只是捕捉到新的位置。 將它放在addAnimation
調用之前和之后,無論有沒有事務,它都沒有任何區別。
委托方法仍然被調用,但您無法直觀地看到任何動畫。
if([notification name] == UIKeyboardWillShowNotification) {
CGRect currBounds = self.loginTable.tableHeaderView.layer.bounds;
CGSize newSize = CGSizeMake(self.loginTable.tableHeaderView.bounds.size.width, 60);
CGPoint newPos = CGPointMake(self.loginTable.layer.position.x, self.loginTable.layer.position.x - 50);
//[CATransaction begin];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
[animation setToValue:[NSValue valueWithCGSize:newSize]];
[animation setDelegate:self];
[self.loginTable.tableHeaderView.layer addAnimation:animation forKey:@"headerShrinkAnimation"];
CABasicAnimation *formPosAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[formPosAnimation setToValue:[NSValue valueWithCGPoint:newPos]];
[formPosAnimation setDelegate:self];
//formPosAnimation.removedOnCompletion = NO;
//formPosAnimation.fillMode = kCAFillModeForwards;
[self.loginTable.layer addAnimation:formPosAnimation forKey:@"tableMoveUpAnimation"];
//[CATransaction commit];
[self.loginTable.tableHeaderView.layer setBounds:CGRectMake(currBounds.origin.x, currBounds.origin.y, newSize.width, newSize.height)];
[self.loginTable.layer setPosition:newPos];
}
我找到了一種方法使它工作,不能說它是否是最好的方法,但它似乎現在正在工作。
關鍵是要結合幾乎所有東西。 所以我必須在動畫上保留removedOnCompletion
和fillMode
,同時還要更新animationDidStop
方法中的位置。 它也可以在不設置兩個動畫參數的情況下工作,但最后你可以看到一個小的閃爍。
- (void)keyboardWillChange:(NSNotification *)notification {
newSize = CGSizeZero;
newPos = CGPointZero;
if([notification name] == UIKeyboardWillShowNotification) {
newSize = CGSizeMake(self.loginTable.tableHeaderView.bounds.size.width, 60);
newPos = CGPointMake(self.loginTable.layer.position.x, self.loginTable.layer.position.x - 50);
} else {
newSize = CGSizeMake(self.loginTable.tableHeaderView.bounds.size.width, 150);
newPos = CGPointMake(self.loginTable.layer.position.x, self.loginTable.layer.position.x + 50);
}
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
[animation setToValue:[NSValue valueWithCGSize:newSize]];
[animation setDelegate:self];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.loginTable.tableHeaderView.layer addAnimation:animation forKey:@"headerShrinkAnimation"];
/*-----------------------------*/
CABasicAnimation *formPosAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[formPosAnimation setToValue:[NSValue valueWithCGPoint:newPos]];
[formPosAnimation setDelegate:self];
formPosAnimation.removedOnCompletion = NO;
formPosAnimation.fillMode = kCAFillModeForwards;
[self.loginTable.layer addAnimation:formPosAnimation forKey:@"tableMoveUpAnimation"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
NSLog(@"Animation did stop");
CGRect currBounds = self.loginTable.tableHeaderView.layer.bounds;
[self.loginTable.tableHeaderView.layer setBounds:CGRectMake(currBounds.origin.x, currBounds.origin.y, newSize.width, newSize.height)];
[self.loginTable.layer setPosition:newPos];
[self.loginTable.tableHeaderView.layer removeAnimationForKey:@"headerShrinkAnimation"];
[self.loginTable.layer removeAnimationForKey:@"tableMoveUpAnimation"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.