UIview animatewithDuration does not works in swift

2019-06-14 18:21发布

问题:

This seems to be a very weird issue. I have a small image view in storyBoard and have added tap gesture to it. On the action of gesture I am trying to add a different image view.

Objective C code-

-(void)tapImage{
    bigImageView = [[UIImageView alloc]init];
    [bigImageView setImage:[UIImage imageNamed:@"main.png"]];

    [bigImageView setFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:bigImageView];


    [UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^(void) {

                         [bigImageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];                         //Leave it empty
                     }
                     completion:^(BOOL finished){

                         // Your code goes here

                     }];

}

Works totally fine.

Swift Code -

func imageTapped()
{
    println("Tapped on Image")

    bigImageView = UIImageView(frame: CGRectMake(0, 0, 50, 50))
    bigImageView.image = UIImage(named: "main")
    self.view.addSubview(self.bigImageView)


    UIView.animateWithDuration(2.0, delay: 2.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {
            self.bigImageView = UIImageView(frame: CGRectMake(0, 0, 320, 500))
        }

    }) { (completed:Bool) -> Void in

    }
}

Does not work. I don't know where am I wrong. :(

UPDATE -

First I change the frame with "bigimageview.frame" so now it was displaying image view in gesture tap. But without animation.

So I removed dipatch on main thread and it was animating.

UIView.animateWithDuration(0.4, delay: 0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

//            dispatch_async(dispatch_get_main_queue()) {
                self.bigImageView.frame = CGRectMake(0, 0, 320, 500)
//            }

            }) { (completed:Bool) -> Void in

        } }

But Still question remains why it happened. Are we not suppose to put UI changes on main thread inside closure/block? Some one please explain.

回答1:

Might be your delay and animation time is same so try this

func imageTapped() {
    println("Tapped on Image")

    bigImageView = UIImageView(frame: CGRectMake(0, 0, 50, 50))
    bigImageView.image = UIImage(named: "main")
    self.view.addSubview(self.bigImageView)


    UIView.animateWithDuration(2.0, delay: 1.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {
            self.bigImageView = UIImageView(frame: CGRectMake(0, 0, 320, 500))
        }

    }) { (completed:Bool) -> Void in

    } }


回答2:

Replace this code

UIView.animateWithDuration(2.0, delay: 1.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {
            self.bigImageView = UIImageView(frame: CGRectMake(0, 0, 320, 500))
        }

    }) { (completed:Bool) -> Void in

    } }

with

UIView.animateWithDuration(2.0, delay: 1.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {

            bigImageView.frame = CGRectMake(0, 0, 320, 500)
        }

    }) { (completed:Bool) -> Void in

    } }


回答3:

viewDidLoad() is not the ideal place where you should animate views. viewDidAppear() method may works.



回答4:

There is nothing to be gained by trying to use dispatch_async(). The way animations work you simply set the attributes in the specified animation block; the values are, in fact, set immediately and iOS uses the GPU where possible to animate the transition. If you run the block of code on a different thread it will be outside the scope of the animation and hence simply have an immediate effect.



回答5:

AnimationWithDuration block takes all of its data(duration, options etc). Your dispatching block simply iterates separately. Thats why its not animating. Reason is simple: everything inside dispatch block iterates separately of other code because of stack system. Frame stopped, dispatch putted to stack, executed, and when it gets back to animation: hop, its already has final value and no animations needed.