开发者

UIView animation performance problem

开发者 https://www.devze.com 2023-01-19 22:47 出处:网络
I\'m doing some animations using the UIView animation and presentModalViewController. Through the simulator it looks fine but on device its rather choppy. Even fairly basic views for example a viewCon

I'm doing some animations using the UIView animation and presentModalViewController. Through the simulator it looks fine but on device its rather choppy. Even fairly basic views for example a viewController with a UISearchBar, UITableView (empty) with a a custom color and a button on the nav bar is jerky when animated through presentModalViewController.

I've tried loading the view into memory on App load and then present it when a button is pressed to see if there is a difference but its 开发者_高级运维the same outcome. As a note I'm creating the objects in code and adding them to the viewControllers view, I'm not using nibs.

There are a couple of views that all I'm animating is a task bar or an alpha property and these animate fine.

Any tricks that might help? Is this something that would be fixed when actually releasing the App?


A lot of UIKit objects render quite slowly (like UITextViews and UISegmentedControllers) so they can look choppy if you try to animate a lot of them at once. Every frame of the animation requires the view to be redrawn.

You can speed up the process by converting these slow views into UIImageViews which can be animated much more smoothly. Try calling the following method right before you do animations:

    [self flattenView:yourView forDuration:0.5];


-(void)flattenView:(UIView *)view forDuration:(CGFloat)duration
{
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView * imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    imgV.image = img;
    [view addSubview:imgV];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [imgV removeFromSuperview];
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消