Device-specific development for IOS: Get and use device posture (Control 3d object display via phone direction)

Source: Internet
Author: User

Using the device's onboard gyroscope can be achieved, when you rotate the phone screen, the inside of the screen will not be updated with the view to move to balance the physical movement.

The following example performs this operation using a small number of simple geometric transformations. He builds a motion manager, subscribes to device motion updates, and then applies image transformations based on the swing, forward tilt, and sway angle of the motion manager return.

@implementationtestbedviewcontroller{Cmmotionmanager*Motionmanager; Uiimageview*ImageView;}- (void) shutdownmotionmanager{NSLog (@"shutting down motion manager");    [Motionmanager stopdevicemotionupdates]; Motionmanager=Nil;}- (void) establishmotionmanager{if(Motionmanager) [self shutdownmotionmanager]; NSLog (@"establishing motion manager"); //Establish the motion managerMotionmanager =[[Cmmotionmanager alloc] init]; if(motionmanager.devicemotionavailable) [Motionmanager startdevicemotionupdatestoqueue:[nsoperationqueue Currentqueue] Withhandler:^ (Cmdevicemotion *motion, Nserror *error)             {Catransform3d transform; Transform= Catransform3dmakerotation (Motion.attitude.pitch,1,0,0); Transform= Catransform3drotate (transform, Motion.attitude.roll,0,1,0); Transform= Catransform3drotate (transform, Motion.attitude.yaw,0,0,1); ImageView.layer.transform=transform; }];}- (void) Viewdidappear: (BOOL) animated{nsstring*imagename = Is_ipad?@"Ipadart.png":@"Iphoneart.png"; UIImage*image =[UIImage Imagenamed:imagename]; ImageView=[[Uiimageview alloc] initwithimage:image]; Imageview.center=Rectcenter (self.view.bounds); [Self.view Addsubview:imageview];}- (void) loadview{[Super Loadview]; Self.view.backgroundColor=[Uicolor Whitecolor];}

Device-specific development for IOS: Get and use device posture (Control 3d object display via phone direction)

Related Article

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.