UIView animation lags on device

北慕城南 提交于 2021-02-08 05:18:35

问题


I'm using animation in my app and get confused, because animation is lagging on device, on simulator everything seems OK. First I tried to use

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

And before commit animation there were something like ~30 lines of code with "if" blocks so I thought this might cause the problem, but then I start to use

[UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveEaseIn
                             animations:^{
                                 mainView.frame = CGRectMake(0, 0, 320, 420);
                                 buttonsView.transform = CGAffineTransformMakeTranslation(0, 68);
                                 radioBar.transform = CGAffineTransformMakeTranslation(0, -50);
                                 vk_controller.view.frame = CGRectMake(0, 0, 320, 440);
                             } 
                             completion:^(BOOL finished){
                                 button.isHiddenDown = YES;
                             }];

in terms of "if" blocks, but lags seems to stay. When I press button there is delay ~0.5-1 sec (why?) and then animation starts. But when I'm on table view

[UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveEaseIn
                             animations:^{
                                 mainView.frame = CGRectMake(0, 0, 320, 420);
                                 buttonsView.transform = CGAffineTransformMakeTranslation(0, 68);
                                 radioBar.transform = CGAffineTransformMakeTranslation(0, -50);
                                 goha_news_controller.view.frame = CGRectMake(0, 0, 320, 420);
                                 goha_news_controller.feed_table.frame = CGRectMake(0, 0, 320, 420);
                                 if(goha_news_controller.backgroundView)
                                 {
                                     goha_news_controller.backgroundView.frame = CGRectMake(0, 0, 320, 420);
                                     goha_news_controller.newsDetailView.frame = CGRectMake(0, 0, 320, 420);
                                 }

                             } 
                             completion:^(BOOL finished){
                                 button.isHiddenDown = YES;
                             }];

in addition to the unexpected delay before the animation, there is harshness animation with bursts.
Can anyone explain why does it happen and how can I fix it?


回答1:


Another possible cause. Are you using shadows in any of the on-screen views or layers? iOS does not handle animating with shadows very well at all.




回答2:


You cannot use the simulator to gauge performance. It has completely different (not just better or worse) performance characteristics than a device (and the devices also differ from generation to generation).

If statements are not causing significant delay; they are very cheap.

Your performance problems probably lie elsewhere. I can't see anything in the code you've shown us that looks like an obvious performance problem.




回答3:


If you resize images in controls while animation, this can cause lags, because image resizing is a very expensive process for CPU. You should make thumbnails of images before you run animation and change images.

Also, try to use Begin animations - commit animations instead of animating using blocks.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
// Here some more animation settings
// Here your animations 
[UIView commitAnimations];


来源:https://stackoverflow.com/questions/10595979/uiview-animation-lags-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!