制作animation来展开和缩小UIView
我想创build一个animation,将调整UIView及其内容的一个因素。 基本上,我想制作一个animation,首先展开视图然后缩小到原始大小。
做这个的最好方式是什么? 我尝试了CALayer.contentScale,但它什么也没做。
你可以像这样嵌套一些animation块:
Objective-C的:
[UIView animateWithDuration:1 animations:^{ yourView.transform = CGAffineTransformMakeScale(1.5, 1.5); } completion:^(BOOL finished) { [UIView animateWithDuration:1 animations:^{ yourView.transform = CGAffineTransformIdentity; }]; }];
Swift 2.0:
UIView.animateWithDuration(1, animations: { () -> Void in yourView.transform = CGAffineTransformMakeScale(1.5, 1.5) }) { (finished: Bool) -> Void in UIView.animateWithDuration(1, animations: { () -> Void in yourView.transform = CGAffineTransformIdentity })}
Swift 3.0:
UIView.animate(withDuration: 1, animations: { yourView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) }) { (finished) in UIView.animate(withDuration: 1, animations: { yourView.transform = CGAffineTransform.identity }) }
并用您自己的replace比例值和持续时间。
这是一个更小的方法也循环:
[UIView animateWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{ yourView.transform = CGAffineTransformMakeScale(1.5, 1.5); } completion:nil];
如果你不想让它保持“呼吸”, UIViewKeyframeAnimationOptionRepeat
选项就是循环的。 animation块作为“吸气”, UIViewKeyframeAnimationOptionAutoreverse
选项自动播放“呼气”animation。