IOS device-specific development: a scrolling view based on an accelerometer

Source: Internet
Author: User

Tilt Rollers Use the device's built-in accelerometer to control movement around the contents of the Uiscrollview. When the user increases the device, the material drops accordingly, he does not position the view on the screen, but instead scrolls the content view to a new offset position.

The challenge of creating this interface is to determine where the device should have his stationary axis, and most people initially suggest that the display should be stable on his back and that the z-axis is pointing straight up. It turns out: this is actually a pretty bad design choice. To use that axis, it means that the screen must actually deviate from the viewer during the navigation. As the device rotates away from the view, the user will not be able to fully see what is happening on the screen, especially when the device is used at a fixed location, and the device sometimes produces this effect when viewed from a high point.

Instead, the following procedure assumes that the stable position is in the direction of approximately 45 degrees through the z axis, where the user places the iphone or ipad in the hands of the natural position, which is facing up and facing forward in the middle position. Tilt back and forth from this skewed position so that the screen remains the most visible during adjustment.

Another change in this program is a much lower acceleration constant. This allows the motion on the screen to occur more slowly, making it easier for users to reduce speed and restore navigation.

#defineSign (_NUM_) ((_num_ < 0)? (-1): 1)#defineMIN (A, B) __nsmin_impl__ (a,b,__counter__)#defineMAX (A, B) __nsmax_impl__ (a,b,__counter__)@implementationtestbedviewcontroller{Uiscrollview*SV; floatXoff; floatXaccel; floatxvelocity; floatYoff; floatYaccel; floatyvelocity;}- (void) Accelerometer: (Uiaccelerometer *) Accelerometer didaccelerate: (Uiacceleration *) acceleration{//Extract the acceleration components    floatxx =-acceleration.x; floatyy = (Acceleration.z +0.5f) *2.0f;//between face up and face forward//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.005f) *Sign (Xaccel); if(Acceldiry = = Newdiry) Yaccel = (ABS (yaccel) +0.005f) *Sign (Yaccel); //Apply Acceleration Changes to the current velocityxvelocity =-xaccel *xx; Yvelocity=-yaccel *yy;}- (void) tick{Xoff+=xvelocity; Xoff= MIN (Xoff,1.0f); Xoff= MAX (Xoff,0.0f); Yoff+=yvelocity; Yoff= MIN (Yoff,1.0f); Yoff= MAX (Yoff,0.0f); CGFloat xsize= Sv.contentSize.width-Sv.frame.size.width; CGFloat ysize= Sv.contentSize.height-Sv.frame.size.height; Sv.contentoffset= Cgpointmake (Xoff * xsize, Yoff *ysize);}- (void) loadview{[Super Loadview]; SV=[[Uiscrollview alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]; Self.view=SV;}- (void) Viewdidappear: (BOOL) animated{nsstring*map =@"http://maps.weather.com/images/maps/current/curwx_720x486.jpg"; Nsoperationqueue*queue =[[Nsoperationqueue alloc] init]; [Queue Addoperationwithblock:^{         //Load The weather dataNsurl *weatherurl =[Nsurl Urlwithstring:map]; NSData*imagedata =[NSData Datawithcontentsofurl:weatherurl]; //Update the image on the main thread using the main queue[[Nsoperationqueue Mainqueue] addoperationwithblock:^{UIImage*weatherimage =[UIImage Imagewithdata:imagedata]; Uiimageview*imageview =[[Uiimageview alloc] initwithimage:weatherimage]; Cgsize initsize=weatherimage.size; Cgsize destsize=weatherimage.size;  while(Destsize.width < (Self.view.frame.size.width *4)) ||(Destsize.height< (Self.view.frame.size.height *4)) {destsize.width+=Initsize.width; Destsize.height+=Initsize.height; } imageview.userinteractionenabled=NO; Imageview.frame= (CGRect) {. Size =Destsize}; Sv.contentsize=destsize;                          [SV Addsubview:imageview]; //Activate The Accelerometer[[Uiaccelerometer Sharedaccelerometer] setdelegate:self]; //Start The physics timer[Nstimer Scheduledtimerwithtimeinterval:0.03ftarget:self selector: @selector (tick) Userinfo:nil Repeats:yes];     }]; }];}

IOS device-specific development: a scrolling view based on an accelerometer

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.