2013-02-23 114 views
1

我有大量的问题试图动画的UISearchBar只是扩大编辑时,所以我想我会提供一个解决方案给谁必须遭受这个问题的任何人。UISearchBar宽度动画和AutoLayout

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

+0

这是如何在swift中实现这个http://stackoverflow.com/a/29565613/2683201 – 2015-04-10 15:50:03

回答

1

大量的试验和错误后,我得到了它由厦门国际银行开启自动版式功能关闭,然后用下面的动画方式工作:

+(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;} 

我希望这有助于和保存人们的一些痛苦,我的必须经过。