简体   繁体   中英

UIView animation doesn't move the view

This is the code I use for the animation.

        NSLog(@"SlideLoadingViewOut : frame: X:%f, Y:%f", self.loadingView.frame.origin.x, self.loadingView.frame.origin.y);
        [UIView animateWithDuration:time animations:^{
            self.loadingView.frame = CGRectMake(320, 0, self.loadingView.frame.size.width, self.loadingView.frame.size.height);
            NSLog(@"SlideLoadingViewOut : frame: X:%f, Y:%f", self.loadingView.frame.origin.x, self.loadingView.frame.origin.y);
        } completion:^(BOOL finished) {
            NSLog(@"SlideLoadingViewOut : frame: X:%f, Y:%f", self.loadingView.frame.origin.x, self.loadingView.frame.origin.y);
        }];

This is the output in the console I get:

Before the animation starts:

SlideLoadingViewOut : frame: X:0.000000, Y:0.000000

In the animation block:

SlideLoadingViewOut : frame: X:320.000000, Y:0.000000

In the completion block:

SlideLoadingViewOut : frame: X:0.000000, Y:0.000000

The view doesn't move. Anyone knows why this might happen.

Wild speculation

for some reason the animation block doesn't like CGRectMake being within the block, try creating it earlier.

Well, someone answered this earlier but for some reason his comment disappeared. Apperntley I was changing the frame on a different thread than the main, I executed it on the main thread using, and it worked.

dispatch_async(dispatch_get_main_queue(), ^{
   // do work here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM