With a little programming, the iphone's airborne accelerometer allows objects to "move" around the screen in real time, responding to the way users tilt their phones. The following code is the creation of an animated butterfly that the user can quickly move across the screen.
The secret of making it work is to add a so-called "physical Timer" to the program. Instead of responding directly to the changes in acceleration, the accelerometer callback is used to measure the current force. It depends on the timer routines over time by changing his picture to apply those forces to the butterflies. Here is a list of key points to keep in mind.
(1). As long as the direction of the force remains the same, the butterflies will accelerate. His speed will be increased in proportion to the acceleration force in the X or Y direction of the measurement.
(2). The Rick routine by the timer will move the butterfly by adding a speed vector to the origin of the butterfly.
(3). The range of butterfly movement is bounded, so when he hits a certain edge, it will stop moving in that direction. You can also always say that butterflies remain on the screen. The Tick method checks the boundary condition.
(4). The butterfly will change its direction and make it always fall. You can implement this by applying a simple rotation transformation in the tick method. When using transformations, also focus on the screen or center offset. Before applying the offset, always reset the math processing and reapply any angle changes. If not, it may cause the picture to be unexpectedly enlarged, shrunk or distorted.
The code is as follows:
#defineSign (_NUM_) ((_num_ < 0)? (-1): 1)#defineRectcenter (Rect) cgpointmake (Cgrectgetmidx (rect), Cgrectgetmidy (rect))@interfaceViewcontroller () {Uiimageview*Butterfly; floatXaccel; floatxvelocity; floatYaccel; floatyvelocity; floatMostrecentangle;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Greencolor]; [Self initbutterfly]; }- (void) initbutterfly{cgsize size; //Load the animation cellsNsmutablearray *butterflies =[Nsmutablearray array]; for(inti =1; I <= -; i++) {NSString*filename = [NSString stringWithFormat:@"Bf_%d.png", I]; UIImage*image =[UIImage Imagenamed:filename]; Size=image.size; [Butterflies addobject:image]; } //Begin the animationbutterfly = [[Uiimageview alloc] initWithFrame: (cgrect) {. size=size}]; [Butterfly setanimationimages:butterflies]; Butterfly.animationduration=0.75f; [Butterfly startanimating]; //Set The butterfly ' s initial speed and accelerationXaccel =2.0f; Yaccel=2.0f; Xvelocity=0.0f; Yvelocity=0.0f; //Add The ButterflyButterfly.center =Rectcenter (self.view.bounds); [Self.view Addsubview:butterfly]; //Activate The Accelerometer[[Uiaccelerometer Sharedaccelerometer] setdelegate:self]; //Start The physics timer[Nstimer Scheduledtimerwithtimeinterval:0.03ftarget:self selector: @selector (tick) Userinfo:nil repeats:yes];}- (void) tick{Butterfly.transform=cgaffinetransformidentity; //Move The butterfly according to the current velocity vectorCGRect rect = Cgrectoffset (Butterfly.frame, Xvelocity,0.0f); if(Cgrectcontainsrect (self.view.bounds, rect)) Butterfly.frame=rect; Rect= Cgrectoffset (Butterfly.frame,0.0f, yvelocity); if(Cgrectcontainsrect (self.view.bounds, rect)) Butterfly.frame=rect; Butterfly.transform= Cgaffinetransformmakerotation (Mostrecentangle +m_pi_2);}- (void) Accelerometer: (Uiaccelerometer *) Accelerometer didaccelerate: (Uiacceleration *) acceleration{//Extract the acceleration components floatxx =-acceleration.x; floatyy =ACCELERATION.Y; Mostrecentangle=atan2 (yy, XX); //Has the direction changed? floatAcceldirx = sign (xvelocity) *-1.0f; floatNewdirx =Sign (XX); floatAcceldiry = sign (yvelocity) *-1.0f; floatNewdiry =Sign (yy); //accelerate. To increase viscosity lower the additive value if(Acceldirx = = Newdirx) Xaccel = (ABS (xaccel) +0.85f) *Sign (Xaccel); if(Acceldiry = = Newdiry) Yaccel = (ABS (yaccel) +0.85f) *Sign (Yaccel); //Apply Acceleration Changes to the current velocityxvelocity =-xaccel *xx; Yvelocity=-yaccel *yy;}
Device-specific development for IOS: Using Accelerators to start objects on the screen