navigationBar和它的titleView rightBarButtonItem随着动画变小了

时间:2013-12-31 07:12:50

标签: ios objective-c animation uinavigationbar uinavigationitem

在正常状态下,用户界面就像是

enter image description here

当tableview向上滑动时,我希望navigationBar变小,带动画。 代码如下

CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"];
shake.duration = DURATION_TIME;

shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0 ,1.0)];
shake.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.5, 1.0)];

shake.removedOnCompletion = NO;
shake.fillMode = kCAFillModeForwards;

[self.navigationController.navigationBar.layer addAnimation:shake forKey:@"shakeAnimation"];

在动画UI之后如下

enter image description here

我不希望navigationBar的y位置发生变化。并且titleView和rightbarItem变得扁平,如何避免它们变平?

1 个答案:

答案 0 :(得分:0)

尝试以下代码。它适用于ios7之前

UIView * testView = self.navigationController.navigationBar ;
CGRect frame = testView.layer.frame ;
testView.layer.anchorPoint = CGPointMake(0.5f, 0.0f) ;
testView.layer.frame = frame ;

[UIView animateWithDuration:1.0 animations:^{
    CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"];
    shake.duration = 1.0;

    shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0 ,1.0)];
    shake.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.5, 1.0)];

    shake.removedOnCompletion = NO;
    shake.fillMode = kCAFillModeForwards;

    [testView.layer addAnimation:shake forKey:@"shakeAnimation"];
}];