2012-01-10 61 views
3

我可以想象當人們看到這個問題時會出現很多嘆息彈出再次。但是,我在這裏,文檔中以及通過Google閱讀了大量信息,但仍未找到解決方案。所以這裏什麼都不做。iOS UIView屬性不用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]; 
} 

回答

2

我已經找到一種方法,使其工作,如果它這樣做的最好辦法不能說,但它似乎是現在的工作。

關鍵是要結合幾乎所有的東西。所以我不得不在我的動畫上保留removedOnCompletionfillMode,同時也更新我的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"]; 

}