此处代码来源于http://blog.sina.com.cn/iphonesdk
UIImageView图片自动切换
-(void)viewDidLoad{ // create the view that will execute our animation UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame]; // load all the frames of our animation campFireView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"campFire01.gif"], [UIImage imageNamed:@"campFire02.gif"], [UIImage imageNamed:@"campFire03.gif"], nil]; // all frames will execute in 1.75 seconds campFireView.animationDuration = 1.75; // repeat the annimation forever campFireView.animationRepeatCount = 0; // start animating [campFireView startAnimating]; // add the animation view to the main window [self.view addSubview:campFireView]; }
按钮打开URL地图
-(IBAction)openMaps{ NSString* addressText = @"1 Queen st, Auckland, NZ"; addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]; NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText]; //NSlog(urlText); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]]; }
图片的持续移动
- (void)viewDidLoad { //定义图片的位置和尺寸,位置:x=10.0f, y=10.0f ,尺寸:x=50.0f, y=40.0f UIImageView *subview = [[UIImageView alloc] initWithFrame: CGRectMake(10.0f, 10.0f, 50.0f, 40.0f)]; //设定图片名称,myPic.png已经存在,拖放添加图片文件到image项目文件夹中 [subview setImage:[UIImage imageNamed:@"image2.jpg"]]; //启用动画移动 [UIImageView beginAnimations:nil context:NULL]; //移动时间2秒 [UIImageView setAnimationDuration:2]; //图片持续移动 [UIImageView setAnimationBeginsFromCurrentState:YES]; //重新定义图片的位置和尺寸,位置 subview.frame = CGRectMake(60.0, 100.0,200.0, 160.0); //完成动画移动 [UIImageView commitAnimations]; //在 View 中加入图片 subview [self.view addSubview:subview]; //使用后释放图片, 使用iOS5,对像为iOS4.3以上可以忽略这步骤 //[subview release]; }
加减法
int count = 0; -(IBAction)reset{ count = 0; counter.text = @"0"; } - (IBAction)addUnit { if(count >= 999) return; NSString *numValue = [[NSString alloc] initWithFormat:@"%d", ++count]; counter.text = numValue; } - (IBAction)subtractUnit { if(count <= 0) return; NSString *numValue = [[NSString alloc] initWithFormat:@"%d", --count]; counter.text = numValue; }