计时器模仿地球绕太阳圆周运动

模仿地球绕圆周运动

#define RADIUS  100
#define CENTER_X  160
#define CENTER_Y  240
//宏定义 : 单纯的替换 ,不会进行计算 ;
#define   num  5+10
#define sum(x,y)  (x+y)

@interface ViewController ()

@end

@implementation ViewController
{
    UIImageView *earth ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView * sun = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fireball"]];
    sun.center = CGPointMake(CENTER_X, CENTER_Y);
    sun.bounds = CGRectMake(0, 0, 62, 62);
    [self.view addSubview:sun];
    
    earth = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"a"]];
    earth.bounds = CGRectMake(0, 0, 30, 30);
    
    //角度  转化 为 弧度
    float huDu =  90/180.0 * M_PI ;
    //圆的参数方程
    //cos(弧度)
    float pointX =  CENTER_X + RADIUS * cos(huDu);
    float pointY =  CENTER_Y  +  1.5 * RADIUS * sin(huDu);
    
    earth.center = CGPointMake(pointX, pointY);
    [self.view addSubview:earth];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)rotate
{
    //修改 角度  ;
    static int angle = 90 ;
    angle ++ ;
    
    float huDu = angle/180.0 * M_PI ;
    //根据新的 弧度 ,计算 x、y坐标
    float pointX =  CENTER_X + RADIUS * cos(huDu)  ;
    float  pointY = CENTER_Y + 1.5 * RADIUS * sin(huDu)  ;
    
    earth.center = CGPointMake(pointX, pointY);
}
效果图如下
计时器模仿地球绕太阳圆周运动_第1张图片

你可能感兴趣的:(计时器模仿地球绕太阳圆周运动)