How can I rotate the image in response to the iPhone accelerometer?

hai, I use an accelerometer to rotate uiimageview through device orientation, like UIInterfaceOrientationLandscapeRight etc. if I do not use the orientation of the device, (if I put the iphone on the table, the accelerometer should work at a certain angle. The device is in UIInterfaceOrientationPortrait), how can I do this? I rotate the image through the center of this image, but I could not understand in which corner it rotates, does it rotate in the center (as the origin)? ( I want a graphical view with a start ), if I want to stop rotation at a certain angle, how can I do this? code: can anyone help me ...?

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { rollingX = (acceleration.x * 0.1) + (rollingX * (1.0 - 0.1)); rollingY = (acceleration.y * 0.1) + (rollingY * (1.0 - 0.1)); float xx = -rollingX; float yy = rollingY; float angle = atan2(yy, xx); self.angle += M_PI / 2.0; if(self.angle >= -2.25 && self.angle <= -0.25) { if(deviceOrientation != UIInterfaceOrientationPortrait) { deviceOrientation = UIInterfaceOrientationPortrait; self.updatingIsEnabled =YES; self.foamView.center = CGPointMake(center.x,center.y); } } else if(self.angle >= -1.75 && self.angle <= 0.75) { if(deviceOrientation != UIInterfaceOrientationLandscapeRight) { deviceOrientation = UIInterfaceOrientationLandscapeRight; self.updatingIsEnabled =YES; } } else if(self.angle >= 0.75 && self.angle <= 2.25) { if(deviceOrientation != UIInterfaceOrientationPortraitUpsideDown) { deviceOrientation = UIInterfaceOrientationPortraitUpsideDown; self.updatingIsEnabled =YES; self.foamView.center = CGPointMake(center.x,center.y); } } else if(self.angle <= -2.25 || self.angle >= 2.25) { if(deviceOrientation != UIInterfaceOrientationLandscapeLeft) { deviceOrientation = UIInterfaceOrientationLandscapeLeft; self.updatingIsEnabled =YES; self.foamView.center = CGPointMake(center.x,center.y); } } } 
+4
source share
2 answers

In this sample code provided by Apple, they use an accelerometer to adjust the orientation of the image in the center of the screen. Hope this code example helps you find what you need. (You will need access to the Apple Developer site to download this sample code)

OalTouch example

Cheers -

+2
source

You can also use a combination of CGAffineTransforms on the view itself. These matrices act to transform the representation by rotation or translations. You can even revitalize the process if you want. If this is what you are actually trying to do, I can provide sample code.

0
source

Source: https://habr.com/ru/post/1285789/


All Articles