2012-01-10 100 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"]; 

}