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

一个地球围绕太阳旋转

 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

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

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

 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

也可以自定义一个继承自 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));
    }
}
时间: 2024-10-09 21:25:35

定时器实现的地球围绕太阳旋转的相关文章

html5中用canvas画八大行星围绕太阳转

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8&

html5 canvas围绕中心点旋转

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

poj 1106(半圆围绕圆心旋转能够覆盖平面内最多的点)

Transmitters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4955   Accepted: 2624 Description In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don't overlap, or at

玩转html5(五)---月球绕着地球转,地球绕着太阳转(canvas实现,同样可以动哦)

关于运动速度的参数与真实速度有点差距,大家可以自行调整 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>地球绕着太阳转,月球绕着地球转</title> </head> <body> <canvas wi

围绕鼠标旋转的炫酷三叶空间旋浆效果

<html><head><title>围绕鼠标旋转的三叶空间旋浆</title><meta content="text/html; charset=gb2312" http-equiv="Content-Type"></head><body bgColor="#000000"><scriptlanguage="JavaScript">

iOS 使用transform和CABasicAnimation实现视图围绕定点旋转和实现一个旋转的圆的动画 以转盘为例

效果图如下 实现的代码 1 #import "TurntableView.h" 2 //#import "TurntableLabel.h" 3 4 @implementation TurntableView 5 { 6 UIImageView *_bgImgV; 7 CGFloat _angle; 8 } 9 - (instancetype)initWithFrame:(CGRect)frame 10 { 11 self = [super initWithFram

android 围绕中心旋转动画

本文主要介绍Android中如何使用rotate实现图片不停旋转的效果.Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果:第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似.本文分析 Tween动画的rotate实现旋转效果. 在新浪微博客户端中各个操作进行中时activity的右上角都会有个不停旋转的图标,类似刷新的效果,给用户以操作中的提示.这种非模态的提示方式推荐使用,那么下面就分享下如何实现这种

unity3d angrybots学习之敌人围绕主角旋转和移动

看过angrybots例子的人都知道,敌人有可能是飞行器,有可能是机器人,这里我们假定他是飞行器,ok,我们首先设定飞行器是rigidbody,然后固定的y的postion,和x,z的rotation,写下如下的代码 using UnityEngine; using System.Collections; public class BuzzeMove : MonoBehaviour { private Transform player;//目标,target private Rigidbody

文字围绕鼠标旋转并移动(滚轮控制半径)

网上看到的字符旋转效果,加以改动跟随鼠标旋转并移动,滚轮控制旋转圆到横向直径和纵向直径: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <META content="text/html; charset=gb231