Thursday, July 8, 2010

Custom UIActionSheet, Those Damn Blue Buttons and Simple Animation

Custom UIActionSheet

CustomUIActionSheet? Sometimes blogs are annoying and the process is hard to follow. But here it's just a matter of grabbing the code in GitHub and making sure to call the viewWillAppear method. Great great great thank you to Andrew Homeyer!

He's got a view sliding up inside a background view. For what I need to do (right now), I ended up doing this:

self.tweakPage.view.frame = self.view.window.frame;
[self.view.window addSubview:self.tweakPage.view];
[self.tweakPage viewWillAppear:YES];

which is to say, you can attach it to the UIWindow at the top of your view hierarchy and then it really is -- for all intents and purposes -- modal.

You can also get into his NIB and put in a close button instead of the one he's got, and get rid of the UINavigationBar... actually you can do whatever you want. The example code is (like) perfect!

Those Cool Blue Buttons

What about the stupid blue buttons that appear in real iPhone apps? This is directly from the Apress book. First, download the icons from here:

and then in your ViewDidLoad method do something like this

UIImage *image = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchedImage = [image stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[self.closeButton setBackgroundImage:stretchedImage forState:UIControlStateNormal];

Then you can mess around with the button's type. I just got a great result setting the type to custom (in IB). Anyway... the stretchable part is the stuff you wouldn't figure out without opening the book.

Tweens on the iPhone

They're tweens. iPhone devs might not know this, but that's what we called them in Flash.

Animation on iPhone is pretty simple, or at least the simple kind is. This is 99% from the CustomUIActionSheet code above.

[UIView beginAnimations:@"someName" context:nil];
// Set delegate and selector to remove from superview when animation completes
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

// move stuff by setting frames to the end positions
[UIView commitAnimations]; 

Then the animationDidStop method looks like this

- (void)animationDidStop:(NSString *)animationID 
                    finished:(NSNumber *)finished context:(void *)context {
    if ([animationID isEqualToString:@"someName"]) {
        // do something here

NOTE: I just noticed that this is behind the times. See this question on SO.

No comments: