2013-02-23 113 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;} 

我希望這有助於和保存人們的一些痛苦,我的必須經過。