Helpex - Trao đổi & giúp đỡ Đăng nhập
16

I need to handle a case where you can do something with or without animation, instead of:

if (animation)
{
    [UIView animateWithBlock:^(){...}];
}
else
{
    ...
}

I want to do:

[UIView animateWithBlock:^(){...} duration:(animation ? duration : 0)]

but not sure if it works, even if it does, is there any overhead for using this instead of directly change the view?

Thanks

16 hữu ích 0 bình luận 6.5k xem chia sẻ
8

Yes, since the duration is zero, the transition will effectively by instantaneous.

8 hữu ích 4 bình luận chia sẻ
29

What I do in this cases, is to create a block that contains all the animations I want to make. Then execute an UIView animation passing the animation block as a parameter, or directly calling that block, whether I want it to be animated or not. Something like this :

void (^animationBlock)();
animationBlock=^{
      // Your animation code goes here
};
if (animated) {
    [UIView animateWithDuration:0.3 animations:animationBlock completion:^(BOOL finished) {

    }];
}else{
    animationBlock();
}

That would avoid the overhead

29 hữu ích 0 bình luận chia sẻ
21

According to the Apple docs:

If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle.

21 hữu ích 3 bình luận chia sẻ
4

I wrote this little Swift extension to overcome the issue:

extension UIView {

/// Does the same as animate(withDuration:animations:completion:), yet is snappier for duration 0
class func animateSnappily(withDuration duration: TimeInterval, animations: @escaping () -> Swift.Void, completion: (() -> Swift.Void)? = nil) {
    if duration == 0 {
        animations()
        completion?()
    }
    else {
        UIView.animate(withDuration: duration, animations: animations, completion: { _ in completion?() })
    }
}
}

One can use it as a replacement for UIView.animate(withDuration:animations:completion) and doesn't have to make any more thoughts about duration 0.

4 hữu ích 0 bình luận chia sẻ
2

Okay, I have further observation on this. Firsth, there is a performance overhead when using the animation with zero duration but the more profound difference is that the animation's completion block is handled async. This means that first hiding and then displaying the view might not get you the result you expected.

So, no, I would definitely suggest not to use the zero as a duration as it's not synchronous.

2 hữu ích 0 bình luận chia sẻ
0

you can set animateWithDuration value dynamically as per requirement.

If You set 0. it means no animation transition time.So, View will be appear without any animation. IF you want provide animation, set some value more than 0.

    **float animationDurationValue=0.03f;
        [UIView animateWithDuration:x delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse 
animations:^{
                             [yourView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
                         }
                         completion:nil];**

Please let me know if any issue.

0 hữu ích 0 bình luận chia sẻ
loading
Không tìm thấy câu trả lời bạn tìm kiếm? Duyệt qua các câu hỏi được gắn thẻ objective-c ios uiview uiviewanimation , hoặc hỏi câu hỏi của bạn.

Có thể bạn quan tâm

loading