Hacintosh 升级到 Snow Leopar... 同样讲《灰姑娘》,美国老师和中国老师的区别
一月 25


In Xcode, click Run > Console to see NSLog statements.

NSLog(@"log: %@ ", myString);
NSLog(@"log: %f ", myFloat);
NSLog(@"log: %i ", myInt);

Display Images

Display an image anywhere on the screen, without using UI Builder. You can use this for other types of views as well.

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

Application Frame

Use "bounds" instead of "applicationFrame" -- the latter will introduce a 20 pixel empty status bar (unless you want that..)

Web view

A basic UIWebView.

CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];

Display the Network Activity Status Indicator

This is the rotating icon displayed on the iPhone status bar in the upper left to indicate there is background network activity taking place.

UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO

Animation: Series of images

Show a series of images in succession.

NSArray *myImages = [NSArray arrayWithObjects:
       [UIImage imageNamed:@"myImage1.png"],
       [UIImage imageNamed:@"myImage2.png"],
       [UIImage imageNamed:@"myImage3.png"],
       [UIImage imageNamed:@"myImage4.gif"],

UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];

Animation: Move an object

Show something moving across the screen. Note: this type of animation is "fire and forget" -- you cannot obtain any information about the objects during animation (such as current position). If you need this information, you will want to animate manually using a Timer and adjusting the x&y coordinates as necessary.

CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:-60];
[view.layer addAnimation:theAnimation forKey:@"animateLayer"];

NSString and int

The following example displays an integer's value as a text label:

currentScoreLabel.text = [NSString stringWithFormat:@"%d", currentScore];

Regular Expressions (RegEx)

There is currently no easy way to do RegEx with the framework. You cannot use any regex involving NSPredicate on iPhone -- it may work in Simulator, but does not work on the device!

Access properties/methods in other classes

One way to do this is via the AppDelegate:

myAppDelegate *appDelegate
       = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[[[appDelegate rootViewController] flipsideViewController] myMethod];
dirk 发表于 2010-01-25 星期一

0 Responses to “iPhone SDK Examples”

  1. 尚无评论

发表评论 »