码迷,mamicode.com
首页 > 其他好文 > 详细

定时器实现的地球围绕太阳旋转

时间:2015-01-03 23:43:17      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

一个地球围绕太阳旋转

技术分享
 1 #import "HUAppDelegate.h"
 2 
 3 #define CENTER_X 160
 4 #define CENTER_Y 240
 5 #define RADIUS 130
 6 
 7 @implementation HUAppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
10 {
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12     self.window.backgroundColor = [UIColor blackColor];
13     [self.window makeKeyAndVisible];
14     
15     UIImageView * sun = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fireball.png"]];
16     sun.bounds = CGRectMake(0, 0, 62, 62);
17     sun.center = CGPointMake(CENTER_X, CENTER_Y);
18     [self.window addSubview:sun];
19     
20     UIImageView * earth = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"earth.png"]];
21     earth.bounds = CGRectMake(0, 0, 31, 31);
22     earth.center = CGPointMake(CENTER_X + RADIUS * cos(0), 1.5 * CENTER_Y + RADIUS * sin(0));
23     earth.tag = 10;
24     [self.window addSubview:earth];
25     
26     [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(ratate) userInfo:nil repeats:YES];
27     
28     return YES;
29 }
30 
31 - (void)ratate
32 {
33     static int angle = 0;
34     angle ++;
35     UIImageView *imageView = (UIImageView *)[self.window viewWithTag:10];
36     imageView.center = CGPointMake(CENTER_X + RADIUS * cos(angle * M_PI / 180), CENTER_Y + 1.5 * RADIUS * sin(angle * M_PI / 180));
37 }
38 
39 @end
View Code

升级版:多个地球围绕太阳旋转(有密集恐惧症的人还真是不能一直盯着看,??)

原理是一样的,只不过是多添加了几个“地球”

技术分享
 1 #import "HUAppDelegate.h"
 2 
 3 #define CENTER_X 160
 4 #define CENTER_Y 240
 5 #define RADIUS 120
 6 
 7 @implementation HUAppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
10 {
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12     self.window.backgroundColor = [UIColor blackColor];
13     [self.window makeKeyAndVisible];
14     
15     UIImageView * sun = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fireball"]];
16     sun.center = CGPointMake(CENTER_X, CENTER_Y);
17     sun.bounds = CGRectMake(0, 0, 70, 70);
18     [self.window addSubview:sun];
19     
20     _earthArray = [[NSMutableArray alloc] init];
21     for (int i = 0; i < 12; i ++)  //有 12 个地球同时旋转
22     {
23         UIImageView *earth = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"earth"]];
24         int angle = 30 * i;
25         float huDu = angle * M_PI / 180;
26         earth.center = CGPointMake(CENTER_X + RADIUS * cos(huDu), CENTER_Y + 1.5 * RADIUS * sin(huDu));
27         earth.bounds = CGRectMake(0, 0, 35, 35);
28         [self.window addSubview:earth];
29         [_earthArray addObject:earth];
30     }
31     
32     [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
33  
34     return YES;
35 }
36 
37 - (void)rotate
38 {
39     static int count = 0;
40     count ++;
41     for (int i = 0; i < 12; i ++)
42     {
43         UIImageView *earth = [_earthArray objectAtIndex:i]; //获取当前地球的起始角度,一遍获取其起始角度
44         int angle = 30 * i + count;  //获取当前地球的起始角度,然后开始运动
45         float huDu = angle * M_PI / 180;
46         earth.center = CGPointMake(CENTER_X + RADIUS * cos(huDu), CENTER_Y + 1.5 * RADIUS * sin(huDu));
47     }
48 
49 }
50 
51 @end
View Code

也可以自定义一个继承自 UIImageView 的类,添加一个记载其当前角度的属性

 @property int angle; 

然后在 rotate 方法里就不需要静态变量来改变“地球”当前的角度

- (void)rotate
{
    for (int i = 0 ; i < _earthArray.count; i ++)
    {
        EarthImageView *earth = [_earthArray objectAtIndex:i];
        earth.angle += 1;
        float huDu = earth.angle * M_PI / 180;
        earth.center = CGPointMake(CENTER_X + RADIUS * cos(huDu), CENTER_Y + 1.5 * RADIUS * sin(huDu));
    }
}

 

定时器实现的地球围绕太阳旋转

标签:

原文地址:http://www.cnblogs.com/hyhl23/p/4200104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!