定时器实现的地球围绕太阳旋转
2015-01-03 22:48
246 查看
一个地球围绕太阳旋转
View Code
也可以自定义一个继承自 UIImageView 的类,添加一个记载其当前角度的属性
@property int angle;
然后在 rotate 方法里就不需要静态变量来改变“地球”当前的角度
#import "HUAppDelegate.h" #define CENTER_X 160 #define CENTER_Y 240 #define RADIUS 120 @implementation HUAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor blackColor]; [self.window makeKeyAndVisible]; UIImageView * sun = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fireball"]]; sun.center = CGPointMake(CENTER_X, CENTER_Y); sun.bounds = CGRectMake(0, 0, 70, 70); [self.window addSubview:sun]; _earthArray = [[NSMutableArray alloc] init]; for (int i = 0; i < 12; i ++) //有 12 个地球同时旋转 { UIImageView *earth = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"earth"]]; int angle = 30 * i; float huDu = angle * M_PI / 180; earth.center = CGPointMake(CENTER_X + RADIUS * cos(huDu), CENTER_Y + 1.5 * RADIUS * sin(huDu)); earth.bounds = CGRectMake(0, 0, 35, 35); [self.window addSubview:earth]; [_earthArray addObject:earth]; } [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotate) userInfo:nil repeats:YES]; return YES; } - (void)rotate { static int count = 0; count ++; for (int i = 0; i < 12; i ++) { UIImageView *earth = [_earthArray objectAtIndex:i]; //获取当前地球的起始角度,一遍获取其起始角度 int angle = 30 * i + count; //获取当前地球的起始角度,然后开始运动 float huDu = angle * M_PI / 180; earth.center = CGPointMake(CENTER_X + RADIUS * cos(huDu), CENTER_Y + 1.5 * RADIUS * sin(huDu)); } } @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)); } }
相关文章推荐
- Transform类 RotateAround() 太阳、地球、月亮围绕旋转
- 一个坐标点围绕任意中心点旋转--C#实现
- [HTML5-SVG]使用svg、CSS3、raphaeljs:模拟月球绕地球,地球绕太阳旋转
- iOS开发--QQ音乐练习,旋转动画的实现,音乐工具类的封装,定时器的使用技巧,SliderBar的事件处理
- canvas实现图片围绕左上角一点进行旋转
- three.js实现围绕某物体旋转
- 一个坐标点围绕任意中心点旋转--C#实现
- Unity3D学习日记(五)实现围绕物品旋转的的摄像机控制
- OPengl实现小球围绕大球旋转的效果
- JavaScript canvas实现围绕旋转动画
- 一个坐标点围绕任意中心点旋转--C#实现
- unity3d学习之Rotate和RotateAround做地球绕太阳旋转
- 玩转html5(五)---月球绕着地球转,地球绕着太阳转(canvas实现,同样可以动哦)
- Unity3d实现物体围绕某一点进行旋转
- CSS3实现围绕任何点来旋转-京东推荐动画
- [OpenGL]使用简单的视角变化实现太阳和地球的转动
- 玩转html5(五)---月球绕着地球转,地球绕着太阳转(canvas实现,同样可以动哦)
- 开玩笑html5(五岁以下儿童)---绕地球月球,地球绕太阳运动(canvas实现,同样可以移动哦)
- Rotate实现围绕中心旋转动画
- VS 平台下 OpenGL 实现地球、月球、太阳运动