The timer realizes the Earth revolves around the sun

Source: Internet
Author: User

One Earth revolves around the sun

1 #import "HUAppDelegate.h"2 3 #defineCenter_x 1604 #defineCenter_y 2405 #defineRADIUS 1306 7 @implementationhuappdelegate8 9-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) LaunchoptionsTen { OneSelf.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; ASelf.window.backgroundColor =[Uicolor Blackcolor]; - [Self.window makekeyandvisible]; -      theUiimageview * Sun = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Fireball.png"]]; -Sun.bounds = CGRectMake (0,0, +, +); -Sun.center =Cgpointmake (center_x, center_y); - [Self.window Addsubview:sun]; +      -Uiimageview * earth = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Earth.png"]]; +Earth.bounds = CGRectMake (0,0, to, to); AEarth.center = Cgpointmake (center_x + RADIUS * cos (0),1.5* center_y + RADIUS * sin (0)); atEarth.tag =Ten; - [Self.window Addsubview:earth]; -      -[Nstimer Scheduledtimerwithtimeinterval:0.01target:self selector: @selector (ratate) Userinfo:nil Repeats:yes]; -      -     returnYES; in } -  to- (void) Ratate + { -     Static intAngle =0; theAngle + +; *Uiimageview *imageview = (Uiimageview *) [Self.window Viewwithtag:Ten]; $Imageview.center = Cgpointmake (center_x + RADIUS * cos (angle * M_PI/ the), Center_y +1.5* RADIUS * sin (angle * m_pi/ the));Panax Notoginseng } -  the @end
View Code

Upgrade: More than one Earth rotates around the sun (people with intense phobias can't keep staring, huh?) )

The principle is the same, just add a few more "Earth"

1 #import "HUAppDelegate.h"2 3 #defineCenter_x 1604 #defineCenter_y 2405 #defineRADIUS 1206 7 @implementationhuappdelegate8 9-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) LaunchoptionsTen { OneSelf.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; ASelf.window.backgroundColor =[Uicolor Blackcolor]; - [Self.window makekeyandvisible]; -      theUiimageview * Sun = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Fireball"]]; -Sun.center =Cgpointmake (center_x, center_y); -Sun.bounds = CGRectMake (0,0, -, -); - [Self.window Addsubview:sun]; +      -_eartharray =[[Nsmutablearray alloc] init]; +      for(inti =0; I < A; i + +)//There are 12 simultaneous rotations of the Earth . A     { atUiimageview *earth = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Earth"]]; -         intAngle = -*i; -         floatHudu = angle * M_PI/ the; -Earth.center = Cgpointmake (center_x + RADIUS * cos (hudu), center_y +1.5* RADIUS *sin (hudu)); -Earth.bounds = CGRectMake (0,0, *, *); - [Self.window Addsubview:earth]; in [_eartharray Addobject:earth]; -     } to      +[Nstimer Scheduledtimerwithtimeinterval:0.01target:self selector: @selector (rotate) userinfo:nil repeats:yes]; -   the     returnYES; * } $ Panax Notoginseng- (void) Rotate - { the     Static intCount =0; +Count + +; A      for(inti =0; I < A; i + +) the     { +Uiimageview *earth = [_eartharray objectatindex:i];//get the starting angle of the current Earth and get its starting angle again -         intAngle = -* i + count;//get the starting angle of the current Earth and start moving $         floatHudu = angle * M_PI/ the; $Earth.center = Cgpointmake (center_x + RADIUS * cos (hudu), center_y +1.5* RADIUS *sin (hudu)); -     } -  the } - Wuyi @end
View Code

You can also customize a class that inherits from Uiimageview, adding a property that records its current angle

@property int angle;

Then in the Rotate method, no static variables are needed to change the current angle of the Earth.

-(void) rotate{    for (int0 ; i < _eartharray.count; i + + )    {         *earth = [_eartharray objectatindex:i];         1 ;         float  the ;         1.5 * RADIUS * sin (hudu));}    }

The timer realizes the Earth revolves around the sun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.