UISearchBar宽度动画和AutoLayout

时间:2013-02-23 16:11:33

标签: ios animation uisearchbar autolayout

在编辑UISearchBar时,我遇到了大量问题,只是简单地扩展,所以我想我会为那些不得不忍受这个问题的人提供解决方案。

在动画UISearchBar时已经有problems,并且在将其与iOS AutoLayout混合时问题会进一步增加。如果您遇到了同样的问题,那么我已经在下面发布了解决方案。它可能不完美但它确实有效。

1 个答案:

答案 0 :(得分:1)

经过多次试验和错误后,我通过在xib中关闭AutoLayout功能然后使用下面的Animation方法让它工作:

+(CAAnimationGroup *)changeView:(UIView *)view frameTo:(CGRect)frame{
CGRect oldFrame = view.frame;

// /2.0 because size animation occurs from the anchor point which is set to (0.5,0.5) by default
CGPoint oldOrigin = CGPointMake(oldFrame.origin.x+oldFrame.size.width/2.0, oldFrame.origin.y+oldFrame.size.height/2.0);
CGPoint newOrigin = CGPointMake(frame.origin.x+frame.size.width/2.0, frame.origin.y+frame.size.height/2.0);

CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

positionAnimation.fromValue = [NSValue valueWithCGPoint:oldOrigin];
positionAnimation.toValue = [NSValue valueWithCGPoint:newOrigin];
view.layer.position = newOrigin;

CABasicAnimation *sizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];

sizeAnimation.fromValue = [NSValue valueWithCGRect:oldFrame];
sizeAnimation.toValue = [NSValue valueWithCGRect:frame];
view.layer.bounds = frame;

CAAnimationGroup *frameChangeAnimationGroup = [CAAnimationGroup animation];

frameChangeAnimationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
frameChangeAnimationGroup.animations = [NSArray arrayWithObjects:positionAnimation,sizeAnimation, nil];

[view.layer addAnimation:frameChangeAnimationGroup forKey:@"frame"];

return frameChangeAnimationGroup;}

我希望这有助于为人们带来一些我必须经历的痛苦。