动画UIView增长和缩小?

时间:2015-05-14 22:50:17

标签: ios objective-c swift

我有UIView

如何为此视图设置动画,使视图变大(宽度,高度)然后再缩小到原始大小?

4 个答案:

答案 0 :(得分:2)

只需应用UIView动画,然后应用动画的反面来恢复原始视图。

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];

[UIView animateKeyframesWithDuration:1.0
                               delay:0.0
                              options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
                           animations:^{

                              myView.frame = CGRectMake(0,0,200,300);

                           } completion:^(BOOL finished){

}];

答案 1 :(得分:2)

快速回答......

//Get Smaller
UIView.animateWithDuration(1.0, animations: {
self.view.frame.size.height -= 100
self.view.frame.size.width -= 100


})
//grow
UIView.animateWithDuration(1.0, animations: {
self.view.frame.size.height += 100
self.view.frame.size.width += 100


})

答案 2 :(得分:1)

只需使用transform的scale属性执行UIView animation

目标-C:

[UIView animateWithDuration:1
                 animations:^{
                     yourView.transform = CGAffineTransformMakeScale(1.5, 1.5);
                 }
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:1
                                      animations:^{
                                          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
    })
}

CGAffineTransformMakeScale缩放视图,CGAffineTransformIdentity将视图恢复为原始状态。

答案 3 :(得分:1)

使用CAKeyframeAnimation进行动画增长和缩小的简单方法

    let animation = CAKeyframeAnimation(keyPath: "transform.scale")

    animation.values = [1.0, 1.2, 1.0]
    animation.keyTimes = [0, 0.5, 1]
    animation.duration = 1.5
    animation.repeatCount = Float.infinity
    view.layer.add(animation, forKey: nil)