我正在尝试开发一个应用程序,该应用程序具有类似于“我周围”的位置列表功能,侧面带有小方向箭头。多亏了 Stackoverflow,不同位置的方位和偏移不是问题,并且通过以下教程补偿罗盘滞后做得很好:http: //www.sundh.com/blog/2011/09/stabalize-compass-of-带陀螺仪的iphone/
所有的东西都可以在该 UITableView 中的一个位置正常工作。但是当有多个位置时,箭头不会变得平滑,感觉我的 iPhone 不够快,无法计算这些东西并转动这些多个箭头,但我不知道如何做得更好。
目前我正在尝试这个(没有位置特定的方向偏移):
- 我将所有单元格的所有 UIImageViews 保存在一个数组中
当获得一个新的偏航值时,我遍历数组并实现所有图像旋转
if(motionManager.isDeviceMotionAvailable) { // Listen to events from the motionManager motionHandler = ^ (CMDeviceMotion *motion, NSError *error) { CMAttitude *currentAttitude = motion.attitude; float yawValue = currentAttitude.yaw; // Use the yaw value // Yaw values are in radians (-180 - 180), here we convert to degrees float yawDegrees = CC_RADIANS_TO_DEGREES(yawValue); currentYaw = yawDegrees; // We add new compass value together with new yaw value yawDegrees = newCompassTarget + (yawDegrees - offsetG); // Degrees should always be positive if(yawDegrees < 0) { yawDegrees = yawDegrees + 360; } compassDif.text = [NSString stringWithFormat:@"Gyro: %f",yawDegrees]; // Debug float gyroDegrees = (yawDegrees*radianConst); // If there is a new compass value the gyro graphic animates to this position if(updateCompass) { [self setRotateArrow:gyroDegrees animated:YES]; [self commitAnimations]; updateCompass = 0; } else { [self setRotateArrow:gyroDegrees animated:NO]; [UIView commitAnimations]; } };
和 setRotateArrow:animated 方法:
- (void) setRotateArrow:(float)degrees animated:(BOOL)animated{
UIImage *arrowImage = [UIImage imageNamed:@"DirectionArrow.png"];
for (int i = 0; i<arrowImageViews.count; i++) {
[(UIImageView *)[arrowImageViews objectAtIndex:i] setImage:arrowImage];
CGFloat arrowTransform = degrees;
//Rotate the Arrow
CGAffineTransform rotate = CGAffineTransformMakeRotation(arrowTransform);
[(UIImageView *)[arrowImageViews objectAtIndex:i] setTransform:rotate];
}
}
如果有人知道如何在设备旋转之后平稳地使箭头旋转,我将非常感激。