views:

410

answers:

2

I am developing my first iPhone application, and I have made it past the "machinery" and now I am wrapping up fit & finish details.

I have a view that slides in from the bottom of the screen for my settings pane. The view itself has an opacity of 0, and has a UIImageView view inside of it with a PNG creating the actual background for the view, behind the controls. I am using the built in animation calls for UIView, as in;

[self.view addSubview:settingsViewController.view];

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

frame.origin.y = 200.0;
settingsViewController.view.frame = frame;

[UIView commitAnimations];

Unfortunately, the animation on the device is choppy. The PNG I am using as my background is transparent only in the uppermost 30 pixels across it's width. If I use a PNG with no transparency, the animation is smooth.

The application itself is highly graphical, so the view is animating out over top of a screen full of other PNGs.

I would be extremely surprised if the device can't handle this type of animation smoothly, which leads me to believe I need to refine my approach. Thoughts I had so far include;

Perhaps I need to use Core Animation directly? Would caching the view behind the settings pane before animating be beneficial somehow?

This is new territory for me, and I know there must be ways to optimize that are considered good practice. Thanks for your time!

+1  A: 

I've had some issues with animating the frame in the past, try animating the center property instead.

Also what do you need the transparency in the background image for? Won't it be possible for you to just use that image as the background of the containing view and eliminate the need for the UIImageView?

nduplessis
Thanks for your reply. I wasn't aware that I could draw the image in directly. So I did some quick reading, and overrode drawRect for my settings pane UIView. That works. I don't understand your meaning on the transparency though, if I don't set the opacity to 0 the PNG is drawn on a solid backgrnd.
Carson C.
I am setting the transparency in IB, for clarity.
Carson C.
You don't even needed to override drawRect, you could just do myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mybg.png"]];What I want to know about the transparency is what you need to show behind it if not the solid colour?
nduplessis
The application being as chock-full of custom graphics as it is, I wanted to carry the style through to the settings view. When it slides up, it is an irregular shape on top and only slides up part way. The view below is therefore still visible. Is there a better way to achieve this effect?
Carson C.
OK, I see. No there isn't really another way. Have you tried animating the center property instead of the frame?
nduplessis
I did try colorWithPatternImage and it doesn't seem to support the transparency (it renders as black). At the moment I am animating via the center of the frame as you suggested, and drawing with drawRect to get the desired effect. No smoothness change yet... Thank you though for the tips!
Carson C.
+1  A: 

It won't matter if you use Core Animation "directly" or animate the center property: it is choppy because the graphics hardware has to composite the transparent image with the background on every frame of the animation. You said yourself that using a completely opaque PNG makes the animation smooth.

Try running the app under Instruments, the Core Animation tool has some checkboxes which highlight composited areas in real time and that might give you some hints about re-doing your drawing.

If only the top 30 pixels need to be transparent, it might be worth splitting the background into two views, one 30 pixel strip at the top (transparent) and the rest (opaque). If it is compositing the entire view every frame, that change might be enough to let it know it only has to composite only the 30 pixels at the top.

benzado
Of course I'm aware of the difference in processor overhead for compositing transparency vs opaque images. A good suggestion on splitting the image. I'll take a look into that. The reality is, I may just be being overly picky. Instruments tells me everything is running smoothly, my eye disagrees.
Carson C.
Instruments isn't going to tell you your animations are smooth or not. What it does is highlight the graphics in red if they are being composited and green if they are being drawn directly. Use that to see if more is being composited than necessary.
benzado