从UIProgressView获取当前值,并在达到阈值时触发事件



我有几个UIProgressView,用于读取连接到iOS设备的外部设备的电压。视图将其从0-5伏缩放,我想知道是否有方法检索UIProgressView的当前进度/电压,并将其与用户输入的阈值进行比较。一旦超过这个阈值;应该调用我编写的方法。

问题不一定是比较部分或调用方法。我在检索progressview当前值的实时更新时遇到问题。

谢谢你抽出时间。

第二部分编辑:

所以问题是我没有对此进行编码,这是与一种名为Nanospark板的特定产品有关的代码。该板是工厂系统的输入/输出控制板,由各种iOS设备运行。

我似乎不太明白以前的编码器在这个类中做了什么:

//
//  AnalogVC.m
//  NanosparkController
//

#import "AnalogVC.h"
#import "AppDelegate.h"

@interface InputItem : NSObject
@property (weak,nonatomic) UISwitch *onSwitch;
@property (weak,nonatomic) UIProgressView *progressView;
@property (weak,nonatomic) UILabel *valueLabel;
@end

@implementation InputItem
AppDelegate *appDelegate;
+ (id)itemWithSwitch:(id)temp progress:(id)progress label:(id)label
{

InputItem *item = [[InputItem alloc] init];
item.onSwitch = temp;
item.progressView = progress;
item.valueLabel = label;
return item;
}

- (void)setDisconnected
{

self.onSwitch.on = NO;
self.onSwitch.enabled = NO;
self.valueLabel.text = @"0.000 v";
self.progressView.progress = 0;

}

- (void)setOn
{

self.onSwitch.on = YES;
self.onSwitch.enabled = YES;
self.valueLabel.text = @"0.000 v";
self.progressView.progress = 0;
[appDelegate watchPins:@"testing ON"];
}

- (void)setOff
{

self.onSwitch.on = NO;
self.onSwitch.enabled = YES;
self.valueLabel.text = @"0.000 v";
self.progressView.progress = 0;
[appDelegate watchPins:@"testing OFF"];
}

- (void)setValue:(double)value
{
if (self.onSwitch.on)
{
self.valueLabel.text = [NSString stringWithFormat:@"%0.3f v",value];
self.progressView.progress = value/5.0;
if(value > 0.8){
[appDelegate watchPins:@"testing VALUE"];
}
}
}
@end

@interface AnalogVC ()
{

NSArray *_inputItems;
AppDelegate *appDelegate;
NSMutableArray *channel0Values;

UIColor *custom1;
UIColor *custom2;
UIColor *custom3;
UIColor *custom4;
}

@property (nonatomic) NCBoardManager *manager;

@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch0;
@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch1;
@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch2;
@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch3;
@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch4;
@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch5;

@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress0;
@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress1;
@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress2;
@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress3;
@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress4;
@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress5;

@property (weak,nonatomic) IBOutlet UILabel *inputValue0;
@property (weak,nonatomic) IBOutlet UILabel *inputValue1;
@property (weak,nonatomic) IBOutlet UILabel *inputValue2;
@property (weak,nonatomic) IBOutlet UILabel *inputValue3;
@property (weak,nonatomic) IBOutlet UILabel *inputValue4;
@property (weak,nonatomic) IBOutlet UILabel *inputValue5;

@property (weak,nonatomic) IBOutlet UISlider *outputSlider0;
@property (weak,nonatomic) IBOutlet UISlider *outputSlider1;

@property (weak,nonatomic) IBOutlet UIStepper *outputStepper0;
@property (weak,nonatomic) IBOutlet UIStepper *outputStepper1;

@property (weak,nonatomic) IBOutlet UILabel *outputValue0;
@property (weak,nonatomic) IBOutlet UILabel *outputValue1;


- (IBAction)inputChannelChanged:(UISwitch *)sender;
- (IBAction)outputSliderMoved:(UISlider *)sender;
- (IBAction)outputSliderStopped:(UISlider *)sender;
- (IBAction)outputStepperChanged:(UIStepper *)sender;
@end

@implementation AnalogVC{}


//////////////////////////////
#pragma mark View Lifecycle
//////////////////////////////


- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Analog VC loaded");
_inputItems = @[[InputItem itemWithSwitch:_inputSwitch0 progress:_inputProgress0 label:_inputValue0],
[InputItem itemWithSwitch:_inputSwitch1 progress:_inputProgress1 label:_inputValue1],
[InputItem itemWithSwitch:_inputSwitch2 progress:_inputProgress2 label:_inputValue2],
[InputItem itemWithSwitch:_inputSwitch3 progress:_inputProgress3 label:_inputValue3],
[InputItem itemWithSwitch:_inputSwitch4 progress:_inputProgress4 label:_inputValue4],
[InputItem itemWithSwitch:_inputSwitch5 progress:_inputProgress5 label:_inputValue5]];

_manager = [NCBoardManager sharedBoardManager];

__unsafe_unretained AnalogVC *vc = self;
[_manager setAnalogInputHandling:dispatch_get_main_queue()
filter:^(NCAnalogInputs *inputs){ return YES; }
handler:^(NCAnalogInputs *inputs){ [vc setAnalogInputs:inputs]; }];


//  Register for notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(boardConnected:)
name:CONNECTED_NOTIFICATION
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(boardDisconnected:)
name:DISCONNECTED_NOTIFICATION
object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateAnalogInputs];
[self updateAnalogOutputs];

custom1 = [UIColor whiteColor];
custom2 = [UIColor darkGrayColor];
custom3 = [UIColor blackColor];
custom4 = [UIColor colorWithRed:.97 green:.97 blue:.588 alpha:1.0];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[custom2 CGColor], (id)[custom1 CGColor], (id)[custom2 CGColor], nil];
gradient.startPoint = CGPointMake(0.5, 0);
gradient.endPoint = CGPointMake(0.5, 1.0);
gradient.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil];
[self.view.layer insertSublayer:gradient atIndex:0];

[self.inputSwitch0 setOnTintColor:custom4];
[self.inputSwitch1 setOnTintColor:custom4];
[self.inputSwitch2 setOnTintColor:custom4];
[self.inputSwitch3 setOnTintColor:custom4];
[self.inputSwitch4 setOnTintColor:custom4];
[self.inputSwitch5 setOnTintColor:custom4];

[self.inputSwitch0 setTintColor:custom3];
[self.inputSwitch1 setTintColor:custom3];
[self.inputSwitch2 setTintColor:custom3];
[self.inputSwitch3 setTintColor:custom3];
[self.inputSwitch4 setTintColor:custom3];
[self.inputSwitch5 setTintColor:custom3];

self.inputProgress0.trackTintColor = custom3;
self.inputProgress1.trackTintColor = custom3;
self.inputProgress2.trackTintColor = custom3;
self.inputProgress3.trackTintColor = custom3;
self.inputProgress4.trackTintColor = custom3;
self.inputProgress5.trackTintColor = custom3;

self.inputProgress0.progressTintColor = custom4;
self.inputProgress1.progressTintColor = custom4;
self.inputProgress2.progressTintColor = custom4;
self.inputProgress3.progressTintColor = custom4;
self.inputProgress4.progressTintColor = custom4;
self.inputProgress5.progressTintColor = custom4;

self.outputSlider0.minimumTrackTintColor = custom4;
self.outputSlider1.minimumTrackTintColor = custom4;
self.outputSlider0.maximumTrackTintColor = custom3;
self.outputSlider1.maximumTrackTintColor = custom3;

self.outputSlider0.thumbTintColor = custom3;
self.outputSlider1.thumbTintColor = custom3;

if(_manager.isBoardConnected)
{
self.outputStepper0.tintColor = custom4;
self.outputStepper1.tintColor = custom4;
self.outputStepper0.enabled = TRUE;
self.outputStepper1.enabled = TRUE;
self.outputSlider0.enabled = TRUE;
self.outputSlider1.enabled = TRUE;
}
else
{
self.outputStepper0.tintColor = custom2;
self.outputStepper1.tintColor = custom2;
self.outputStepper0.enabled = FALSE;
self.outputStepper1.enabled = FALSE;
self.outputSlider0.enabled = FALSE;
self.outputSlider1.enabled = FALSE;
}
}


//////////////////////////////
#pragma mark Rotation Calls
//////////////////////////////


- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
return FALSE;
}


//////////////////////////
#pragma mark Board Calls
//////////////////////////


- (void)boardConnected:(NSNotification *)notification
{
[self updateAnalogInputs];
[self updateAnalogOutputs];

self.outputStepper0.enabled = TRUE;
self.outputStepper1.enabled = TRUE;
self.outputSlider0.enabled = TRUE;
self.outputSlider1.enabled = TRUE;

self.outputStepper0.tintColor = custom4;
self.outputStepper1.tintColor = custom4;
}

- (void)boardDisconnected:(NSNotification *)notification
{
[self updateAnalogInputs];
[self updateAnalogOutputs];

self.outputStepper0.enabled = FALSE;
self.outputStepper1.enabled = FALSE;
self.outputSlider0.enabled = FALSE;
self.outputSlider1.enabled = FALSE;

self.outputStepper0.tintColor = custom2;
self.outputStepper1.tintColor = custom2;
}

- (void)updateAnalogInputs
{

uint8_t channel = self.manager.analogInputChannels;
switch (self.manager.analogInputStatus)
{
case NCInputConnected:
//  Check if channels we left on
if (channel) self.manager.analogInputChannels = 0;
[_inputItems makeObjectsPerformSelector:@selector(setOff)];
break;
case NCInputDisconnected:
[_inputItems makeObjectsPerformSelector:@selector(setDisconnected)];
break;
case NCInputLiveUpdating:
for (InputItem *item in _inputItems)
{
//if (channel & 1) [item setOn];
//else             [item setOff];
channel >>= 1;
}
break;
case NCInputSampling:
[_inputItems makeObjectsPerformSelector:@selector(setDisconnected)];
break;
case NCInputTransfering:
[_inputItems makeObjectsPerformSelector:@selector(setDisconnected)];
break;
}

}


- (void)setAnalogInputs:(NCAnalogInputs *)inputs
{

int i = 0;
uint8_t channels = inputs.channels;
for (InputItem *item in _inputItems)
{
if (channels & 1)
{
[item setValue:[inputs valueForChannel:i]];

}
channels >>= 1;
i++;
}
}

- (void)updateAnalogOutputs
{
BOOL connected = [self.manager isBoardConnected];

self.outputSlider0.value = self.manager.analogOutput0;
self.outputSlider0.enabled = connected;
self.outputStepper0.value = self.outputSlider0.value * 1000;
self.outputStepper0.enabled = connected;
self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",self.outputSlider0.value];

self.outputSlider1.value = self.manager.analogOutput1;
self.outputSlider1.enabled = connected;
self.outputStepper1.value = self.outputSlider1.value * 1000;
self.outputStepper1.enabled = connected;
self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",self.outputSlider1.value];
}


///////////////////////////////
#pragma mark IBAction Methods
///////////////////////////////

- (IBAction)inputChannelChanged:(UISwitch *)sender
{
NSLog(@"TEST");
InputItem *item = [_inputItems objectAtIndex:sender.tag];
uint8_t channels = self.manager.analogInputChannels;
if (sender.on)
{
channels  |= (1 << sender.tag);
[item setOn];

}
else
{
channels &= ~(1 << sender.tag);
[item setOff];
}
if (!self.manager.analogInputChannels) [self.manager startAnalogLiveUpdating];
else if(!channels) [self.manager stopAnalogLiveUpdating];

self.manager.analogInputChannels = channels;
}

- (IBAction)outputSliderMoved:(UISlider *)sender
{
if (!sender.tag)
{
self.manager.analogOutput0 = sender.value;
self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",sender.value];
}
else
{
self.manager.analogOutput1 = sender.value;
self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",sender.value];
}
}

- (IBAction)outputSliderStopped:(UISlider *)sender
{
if (!sender.tag)
{
self.manager.analogOutput0 = sender.value;
self.outputStepper0.value = round(sender.value * 1000.0);
self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",self.outputStepper0.value/1000.0];
}
else
{
self.manager.analogOutput1 = sender.value;
self.outputStepper1.value = round(sender.value * 1000.0);
self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",self.outputStepper1.value/1000.0];
}
}

- (IBAction)outputStepperChanged:(UIStepper *)sender
{
float value = sender.value/1000.0;

if (!sender.tag)
{
self.manager.analogOutput0 = value;
self.outputSlider0.value = value;
self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",value];
}
else
{
self.manager.analogOutput1 = sender.value/1000.0;
self.outputSlider1.value = value;
self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",value];
}
}

@end

我遇到的问题是,我不知道如何在故事板上的UIProgressViews之间取值(就像你说的,这是一个微不足道的概念)。然而,有了这种设置,它就相当复杂了。

第二个问题是;我不确定是否有办法对此进行调试,因为应用程序只有在外部设备(纳米公园控制器板)连接到iPod时才能运行

我遇到的最后一个问题是,我假设正在调用IBAction InputChannelChanged(不能像前面提到的那样通过断点定期调试,因为它需要外部设备来运行应用程序),但当我运行应用程序时,它会做它应该做的一切,按钮会对原始软件开发人员的编码做出正确的反应。

这意味着,如果我将我的短信方法添加到IBAction中(通过添加[appDelegate watchPins@"TEST"]),它不会将文本发送给用户,但按钮仍然会按照以前开发人员的愿望做他们应该做的事情。。。。这意味着IBAction方法确实被称为。。。但是为什么我的短信没有通过呢?我知道[appDelegate watchPins:@"TEST"];应该像我在他的其他几门课上使用过的那样有效。

这是一张显示AnalogVC.m:用户界面的屏幕截图

https://i.stack.imgur.com/NNpZk.png

不要觉得有义务回答所有这些问题,只是我觉得有必要提供这三个问题,以便更好地了解问题的背景。对TL表示感谢和歉意;DR.

编辑2:我会上传图片,但我没有要求的最低信誉。

编辑3:我试图添加另一个IBAction,只是想看看按钮是否对此做出反应;即使我从其他工作类中复制了确切的代码,仍然一无所获。

解决方案1:

当您更新UIProgressView时,您需要调用您的方法。我假设使用的是代码中的setProgress: animated:方法来更新进度视图。如果是,请在更新UIProgressView:的方法中尝试此代码

float myVoltageFromDevice = self.deviceInput / 5.0; //this can be whatever your input is mapped to 0.0...1.0 
//the self.device input variable should be the input from the external device.
if(myFloatFromDevice > myThreshold){  //set my threshold to whatever you would like.
[self doAction];  //do whatever you need to do when the value is surpassed
}else{    
[myProgressView setProgress:myVoltageFromDevice animated: NO];
}

编辑:我在//this can be whatever your input is mapped to 0.0...1.0上面的代码中发表了评论。万一这还不清楚,为了实现映射,你可以做:

float myVoltageFromDevice = self.deviceInput / 5.0; 

因为正如您在OP中所说,设备输入变量应该是0-5之间的值。这使得值从0.0-1.0开始,这是UIProgressView将接受的值的范围。

解决方案2:

如果不能实现以上功能(你真的应该能够做到),你应该使用键值观察(KVO),这在苹果开发者文档中有详细说明。

编辑2:

你发布的代码相当复杂,但我相信你需要编辑的方法是- (void)setAnalogInputs:(NCAnalogInputs *)inputs尝试将一些代码更改为:

for (InputItem *item in _inputItems)
{
if (channels & 1)
{
if([inputs valueForChannel:i] > myThreshold){
[self doAction]; //Do your action here.
}else{
[item setValue:[inputs valueForChannel:i]];
}
}
channels >>= 1;
i++;
}

相关内容

最新更新