开发者

Why is my UIView behaving sluggishly?

I have a UIView controller that has a background image and a UIWebView set to resize automatically on orientation changes via the autoresizeResizingMask property. It runs smoothly in the simulator, but is very sluggish when previewd on the device.

I make small use of the web views CALayer to create a border and a shadow and when I comment these parts out it runs a lot smoother. Is this just par for the course when playing with a views CALayer (which I doubt) or am I implementing these parts incorrectly?

Here's the view did load method

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    NSLog(@"Web project View Controller");
    [super viewDidLoad];

    UIImageView *bg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bg.contentMode = UIViewContentModeCenter;
    bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    bg.image = [UIImage imageNamed:@"fuzzyhalo.png"];
    [self.view addSubview:bg];


    projectView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    projectView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    projectView.layer.backgroundColor = [UIColor blackColor].CGColor;

    projectView.layer.shadowColor = [UIColor blackColor].CGColor;
    projectView.layer.shadowOpacity = 1.0;
    projectView.l开发者_Python百科ayer.shadowRadius = 5.0;
    projectView.layer.shadowOffset = CGSizeMake(0, 0);  

    projectView.layer.borderColor = [UIColor blueColor].CGColor;
    projectView.layer.borderWidth = 2.0;


    CGRect newFrame = self.view.bounds;

    newFrame.origin.x = 40;
    newFrame.origin.y = 40;
    newFrame.size.width = newFrame.size.width - 80;
    newFrame.size.height = newFrame.size.height - 80;

    projectView.frame = newFrame;

    [self.view addSubview:projectView];
}

The background image is also quite large. 1024x1024 72ppi PNG.

Any help greatly appreciated. Thanks.


Shadows are very expensive to draw. In iOS 3.2 and later, there is a special property on CALayer called shadowPath. It's a pre-calculated path that defines the outline of the shadow. This keeps the layer from having to generate a path on the fly using the layer's composited alpha channel. Adding this line should help a lot:

projectView.layer.shadowPath = 
  [[UIBezierPath bezierPathWithRect:projectView.layer.bounds] CGPath];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜