Tuesday, 15 June 2010

ios - having issue with scrolling image with gyroscope -



ios - having issue with scrolling image with gyroscope -

i have unusual problem ipad air !!! , code runs fine on ipad 3 , ipad 4 , iphone 5s , ipod 5th gen , on ipad air , image scrolls automatically without user rotate device , here code :

@property (strong, nonatomic) cmmotionmanager *motionmanager; self.mainscrollview.frame = cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.height); self.mainscrollview.bounces = no; self.mainscrollview.userinteractionenabled = no; //set image view uiimage *image= [uiimage imagenamed:@"your_image_name"]; uiimageview *movingimageview = [[uiimageview alloc]initwithimage:image]; [self.mainscrollview addsubview:movingimageview]; self.mainscrollview.contentsize = cgsizemake(movingimageview.frame.size.width, self.mainscrollview.frame.size.height); self.mainscrollview.contentoffset = cgpointmake((self.mainscrollview.contentsize.width - self.view.frame.size.width) / 2, 0); //inital motionmanager , detec gyroscrope every 1/60 sec //the interval may not need fast self.motionmanager = [[cmmotionmanager alloc] init]; self.motionmanager.gyroupdateinterval = 1/60; //this how fast image should move when rotate device, larger number, less roation required. cgfloat motionmovingrate = 4; //get max , min offset x value int maxxoffset = self.mainscrollview.contentsize.width - self.mainscrollview.frame.size.width; int minxoffset = 0; [self.motionmanager startgyroupdatestoqueue:[nsoperationqueue currentqueue] withhandler:^(cmgyrodata *gyrodata, nserror *error) { if (fabs(gyrodata.rotationrate.y) >= 0.1) { cgfloat targetx = self.mainscrollview.contentoffset.x - gyrodata.rotationrate.y * motionmovingrate; if(targetx > maxxoffset) targetx = maxxoffset; else if (targetx < minxoffset) targetx = minxoffset; self.mainscrollview.contentoffset = cgpointmake(targetx, 0); } }];

it's kind of animation !!! code works fine on other devices ! help ?thanks

could seek following: adds error handling code, error may returning gyroscope, , may homecoming value >0.09; utilize nslog more when testing pick apart code , see values returning.

@property (strong, nonatomic) cmmotionmanager *motionmanager; self.mainscrollview.frame = cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.height); self.mainscrollview.bounces = no; self.mainscrollview.userinteractionenabled = no; //set image view uiimage *image= [uiimage imagenamed:@"your_image_name"]; uiimageview *movingimageview = [[uiimageview alloc]initwithimage:image]; [self.mainscrollview addsubview:movingimageview]; self.mainscrollview.contentsize = cgsizemake(movingimageview.frame.size.width, self.mainscrollview.frame.size.height); self.mainscrollview.contentoffset = cgpointmake((self.mainscrollview.contentsize.width - self.view.frame.size.width) / 2, 0); //inital motionmanager , detec gyroscrope every 1/60 sec //the interval may not need fast self.motionmanager = [[cmmotionmanager alloc] init]; self.motionmanager.gyroupdateinterval = 1/60; //this how fast image should move when rotate device, larger number, less roation required. cgfloat motionmovingrate = 4; //get max , min offset x value int maxxoffset = self.mainscrollview.contentsize.width - self.mainscrollview.frame.size.width; int minxoffset = 0; [self.motionmanager startgyroupdatestoqueue:[nsoperationqueue currentqueue] withhandler:^(cmgyrodata *gyrodata, nserror *error) { // if no error --- if(!error){ nslog(@"no error gyroscope %f",gyrodata.rotationrate.y); if (fabs(gyrodata.rotationrate.y) >= 0.1) { nslog(@"moving image"); cgfloat targetx = self.mainscrollview.contentoffset.x - gyrodata.rotationrate.y * motionmovingrate; if(targetx > maxxoffset) targetx = maxxoffset; else if (targetx < minxoffset) targetx = minxoffset; self.mainscrollview.contentoffset = cgpointmake(targetx, 0); } } // error returned gyro else nslog(@"error recieved %@",error); }];

ios objective-c iphone ipad

No comments:

Post a Comment