当用户的设备处于横向模式时,我试图使CoreMotion正常运行。以下代码适用于纵向模式,但我无法弄清楚如何将其转换为Landscape Left和Landscape Right。如果您熟悉Quaternions和CoreMotion,那么任何帮助都将非常受欢迎。
问题: 有一个包含图像视图的ScrollView,此图像视图只能在任何给定点部分看到,并在用户旋转手机以瞄准它时显示。想象一下,坐在电影院的前排,朝各个方向转头,看看整个画面。
我正在使用yaw变量的quaterion,因为真正的偏航'从态度来看似乎是因为电话被搁置了。
在横向模式下转动手机时,我需要知道四元方程,以计算图像和设备的正确度数。
// set content offset for image inside scrollview
CGFloat offsetX;
if (self.deviceDegreesZ >= 180) {
offsetX = (_maximumXOffset/2) + (_maximumXOffset/2) * (fabs(self.deviceDegreesZ - 180))/90;
} else {
offsetX = (_maximumXOffset/2) - (_maximumXOffset/2) * (fabs(self.deviceDegreesZ - 180))/90;
}
CGFloat offsetY;
if (self.deviceDegreesY >= 90) {
offsetY = (_maximumYOffset/2) + (_maximumYOffset/2) * (fabs(self.deviceDegreesY - 90))/45;
} else {
offsetY = (_maximumYOffset/2) - (_maximumYOffset/2) * (fabs(self.deviceDegreesY - 90))/45;
}
if (offsetX != 0 && offsetY != 0) {
[_scrollView setContentOffset:CGPointMake(offsetX, offsetY) animated:NO];
}
// device angles used to set content offset above and look at imageview
// Quaternion
CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
int thePitch = self.motionManager.deviceMotion.attitude.pitch * (180.0 / M_PI);
int theRoll = self.motionManager.deviceMotion.attitude.roll * (180.0 / M_PI);
int theYaw = radiansToDegrees(asin(2*(quat.x*quat.z - quat.w*quat.y)));
// gravity
CMAcceleration gravity = self.motionManager.deviceMotion.gravity;
double rotation = atan2(gravity.x, gravity.y) - M_PI;
self.imageView.transform = CGAffineTransformMakeRotation(rotation);
// portrait (working)
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
// X
if (quat.y >= 0.5f) {
self.deviceDegreesZ = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
} else if (quat.y <= -0.5f) {
self.deviceDegreesZ = 360 + radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
} else {
self.deviceDegreesZ = 180 - radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
}
// Y
CMAcceleration gravity = self.motionManager.deviceMotion.gravity;
if (gravity.z >= 0) {
self.deviceDegreesY = thePitch; // - abs(theYaw)
} else {
self.deviceDegreesY = 180.f - thePitch; // - abs(theYaw)
}
}
// landscape left
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
// ???
}
// landscape right
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
// ???
}