簡體   English   中英

UISearchBar寬度動畫和AutoLayout

[英]UISearchBar width animation and AutoLayout

我在嘗試使UISearchBar動畫化以在編輯時進行簡單擴展時遇到了很多問題,所以我認為我將為其他必須忍受此問題的人提供解決方案。

為UISearchBar設置動畫時已經存在問題 ,將其與iOS AutoLayout混合使用時,問題會進一步加劇。 如果您遇到相同的問題,那么我已在下面發布了解決方案。 它可能並不完美,但確實可以。

經過多次試驗和錯誤,我通過關閉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;}

我希望這可以幫助並減輕人們不得不經歷的痛苦。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM