Making an animation to expand and shrink an UIView

后端 未结 3 792
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 14:49

I want to create an animation that will resize an UIView and its contents by a factor. Basically, I want to make an animation that first expands the view then shrinks it bac

3条回答
  •  离开以前
    2020-12-28 15:13

    You can nest some animation blocks together like so:

    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:

    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/4/5:

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

    and replacing the scale values and durations with your own.

提交回复
热议问题