![](/img/trans.png)
[英]UISearchBar with custom width disappears during UINavigationBar slide up animation
[英]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.