《30天精通iPhone手机编程》-Day26-弹球

经典的弹球游戏

- (void)viewDidLoad {
	[super viewDidLoad];
	//定义图形位置变量对象的数值,此处数值表明图像的动态移动距离
	pos = CGPointMake(14.0, 7.0);
	[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
//定时期对象选择器方法
-(void) onTimer {
	float x = ball.center.x;
	float y = ball.center.y;
	//表明图像的动态移动距离为中心位置加上动态移动距离变量对象的数值
	ball.center = CGPointMake(ball.center.x+pos.x,ball.center.y+pos.y);
	if(ball.center.x > 305 || ball.center.x < 15)
		pos.x = -pos.x;
	if(ball.center.y > 445 || ball.center.y < 15)
		pos.y = -pos.y;	
	//定以跟随移动图像视图的轨迹图像视图文件
	UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ballPath.png"]];
	//定义轨迹图像视图开始时的坐标位置和尺寸大小
	imageView.frame = CGRectMake(x-16, y-16, 32, 32);
	[self.view addSubview:imageView];
	//动画开始
	[UIView beginAnimations:nil context:imageView];
    //持续时间为3秒
	[UIView setAnimationDuration:3.0];
    //缓慢进出动画效果
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    //轨迹视图结束时的坐标位置和尺寸大小
	imageView.frame = CGRectMake(x-6, y-6, 12, 12);
	[imageView setAlpha:0.0];
	[UIView commitAnimations];	
	[imageView release];
	//把ball子视图放在最前面
	[self.view bringSubviewToFront:ball];
}
《30天精通iPhone手机编程》-Day26-弹球_第1张图片

你可能感兴趣的:(编程,iPhone,手机,UIView,float,图形)