1

編集時に UISearchBar を単純に拡張するようにアニメーション化しようとすると、大量の問題が発生したため、この問題に苦しんでいる他の人に解決策を提供すると思いました。

UISearchBar のアニメーション化にはすでに問題があり、それを iOS AutoLayout と組み合わせるとさらに問題が増加します。同じ問題が発生した場合は、以下に解決策を投稿しました。完璧ではないかもしれませんが、うまくいきます。

4

1 に答える 1

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

これが私が経験しなければならなかった痛みの一部を人々に助け、救うことを願っています.

于 2013-02-23T16:11:33.430 に答える