ios - 比较速度值问题(我从 CLLocationManager 得到了什么)

标签 ios objective-c xcode

我对这种提问方式有点困惑

我在这里提及我的查询并支持图像的任何方式。你能帮我解决这个问题吗

第 1 步: 我有这样的要求:通过使用 CLLocationManger 委托(delegate)方法,我获取了速度值,例如:

- (void)startLocationUpdates{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = YES;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    [locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
#pragma mark
#pragma mark - CLLocationManagerDelegate
 - (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations{
float speed = locationManager.location.speed;
float speedInKmph = speed * 3.6;   // to convert the speed into kmph.
NSString *speedValue =[NSString stringWithFormat:@"%.f Kmph ",speedInKmph];
self.currentSpeedLblRef.text = speedValue;
self.maxSpeedLblRef.text = speedValue;
}

第 2 步:currentSpeedLblRef,maxSpeedLblRef --> 这些是我的 UILabel

示例:现在我正在开车 --> 我第一次打开应用程序并获得了汽车的当前速度(例如:120 Kmph)然后我还需要在“maxSpeedLblRef”(120 Kmph)中显示相同的值

一段时间后,我当前的车速为 50 Kmph。但我需要在“maxSpeedLblRef”中显示值 --> 最大值 --> 表示 120 kmph 。因为我已经获得了 previos 120 kmph 的值(value)

在那之后,如果我当前的车速为 180 kmph --> 我需要显示“maxSpeedLblRef”值,例如:180 Kmph。因为与 120 Kmph 相比,它是最新的

关闭应用程序然后

如果我重新打开应用程序,我想显示像“maxSpeedLblRef”这样的谷 --> 180 Kmph。因为这个值是之前保存的值

enter image description here

这是我的源代码:Click here link

最佳答案

您在这里遗漏了一些简单的东西!

您需要存储最大速度以供比较 - 您在这里所做的只是每次都使用当前值更新标签。设置类级属性,初始值=0,当当前速度>最大速度时更新。

有几种方法可以存储最大值,以便在您下次打开应用程序时它就在那里。可能最容易使用用户默认值。有许多教程可用 - 试试这个 http://code.tutsplus.com/tutorials/ios-sdk-working-with-nsuserdefaults--mobile-6039

好的 - 使用您的项目代码,这就是您所需要的。

将您的 ViewController.h 更新为此

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *currentSpeedLblRef;
@property (weak, nonatomic) IBOutlet UILabel *maxSpeedLblRef;

// you need to store the max speed
@property float speedMax;

-(void)loadDefaults;
-(void)storeDefaults;

@end

然后在 VIewController.m 中,用这个替换 viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self loadDefaults];

    [self startLocationUpdates];
}

将 locationManager 函数更新为此

- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations{
    float speed = locationManager.location.speed;
    float speedInKmph = speed * 3.6;   // to convert the speed into kmph.
    NSString *speedValue =[NSString stringWithFormat:@"%.f Kmph ",speedInKmph];
    self.currentSpeedLblRef.text = speedValue;

    if (speedInKmph > self.speedMax)
    {
        self.speedMax = speedInKmph;

        [self storeDefaults];
    }
    NSString *speedMaxValue =[NSString stringWithFormat:@"%.f Kmph ",self.speedMax];

    self.maxSpeedLblRef.text = speedMaxValue;
}

最后添加加载/存储功能

- (void)loadDefaults
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.speedMax = [defaults floatForKey:@"SpeedMax"];
}

- (void)storeDefaults
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setFloat:self.speedMax forKey:@"SpeedMax"];
    [defaults synchronize];
}

关于ios - 比较速度值问题(我从 CLLocationManager 得到了什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35080858/

相关文章:

ios - 代码未通过 plist 获取环境变量

XCode 3.2 中的 Cocoa Java 编译失败

ios - 不重复的随机数生成器函数

java - 更改设备上的时间 => 在游戏中作弊

objective-c - 如何在不直接使用 zlib.dylib 的情况下使用 Zlib 压缩数据?

当 PushKit 在应用程序终止时接收推送时,iOS 11 应用程序崩溃

ios - 如何在 Swift/iOS 中创建侧滑照片库?

ios - 来自 UIImage 或来自文件的 vImage - Swift 和 Accelerate 框架

iphone - 切换 View 时奇怪的 UI 行为(滑入)

ios - 如何在cocoapods的版本之间切换?