2

我目前正在开发基于 Graphs 的 iOS 应用程序。为此,我使用了一个名为F3PlotStrip的第三方控件。

实现类似于:

- (void)viewDidLoad
{
    _ecgView = [[F3PlotStrip alloc] initWithFrame:CGRectMake(50, 50, 648, 299)];
    _ecgView.lineColor = [UIColor greenColor];
    _ecgView.lowerLimit = -10.0f;
    _ecgView.upperLimit = 10.0f;
    [self.view addSubview:_ecgView];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(testGraph:) userInfo:nil repeats:YES];
}

- (void)testGraph:(NSTimer *)a_timer
{
    _ecgView.value = 1.0;
}

但我得到的结果如下:

代码 F3

然后我UIView在我的中添加了一个xib并更改了它的类,F3PlotStrip然后将它作为_ecgView.

然后我将代码更改为:

- (void)viewDidLoad
{
    _ecgView.lineColor = [UIColor greenColor];
    _ecgView.lowerLimit = -10.0f;
    _ecgView.upperLimit = 10.0f;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(testGraph:) userInfo:nil repeats:YES];
}

- (void)testGraph:(NSTimer *)a_timer
{
    _ecgView.value = 1.0;
}

这一次,我得到了实际的输出:

IB F3

但是为什么当我通过代码添加视图时会出现问题?我不能依赖第二种方法(通过 IB),因为我需要动态添加图表。有没有办法解决这个问题?或者这是 F3PlotStrip 的错误?

4

1 回答 1

1

我今天早上开始使用这个小部件。我现在在 iPhone 上躺在床上,所以无法发布我想发布的内容,但我会在早上上班时发布。

我已经设法使用它并使用动态添加的 UIView 执行您想要执行的操作,或者我应该说 F3PlotStrip。

我也使 .h 和 .m 符合 ARC 标准。不确定你是否这样做了。

明天我将完成一些不错的代码,这些代码使用 NSTimer 读取 NSArray 的内容以动态绘制,并带有动画。再次使用视图作为子视图添加到我的视图控制器中。

明天没有详细查看您的代码,因为我已经设法开始工作。

好的 - 我在工作,坐在 Xcode 前面。这是我用作此小部件的基本测试平台的 VC。昨晚我很快就把它敲了起来,但完成了工作。最初,我在我的 Storyboard 第一个 VC 中添加了一个 UIView,并将其子类化为 F3PlotStrip。但是,鉴于我看到您在动态添加 UIView 时遇到问题,我想我会试一试。你和我的唯一区别是我在 alloc-init 之后设置 UIView 的框架。看你怎么想。它使用 Sparkline 方法以及将图形动画显示到屏幕上。

//
//  PlotViewController.m
//  PlotTest
//
//  Created by Carl Hine on 16/05/2013.
//  Copyright (c) 2013 Carl Hine. All rights reserved.
//

#define COLOUR_POWDER_BLUE                  [[UIColor alloc] initWithRed:176/255.0f     green:224/255.0f blue:230/255.0f alpha:1]

#import "F3PlotStrip.h"
#import "PlotViewController.h"

@interface PlotViewController ()

@property (strong, nonatomic) IBOutlet F3PlotStrip *IBOPLotView;
@property (strong, nonatomic) NSTimer *plotStipTimer;
@property (strong, nonatomic) NSArray *plotData;
@property (nonatomic) NSInteger plotDataArrayIdx;
@property (strong, nonatomic) F3PlotStrip *plotView2;

@end

@implementation PlotViewController

@synthesize plotStipTimer;
@synthesize plotData;
@synthesize plotDataArrayIdx;

BOOL bUsingDynPlot = YES;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)testGraph:(NSTimer *)a_timer {

    float RSEFigure = [[self.plotData objectAtIndex:plotDataArrayIdx] floatValue];

    if (bUsingDynPlot)
        _plotView2.value = RSEFigure;
    else
        _IBOPLotView.value = RSEFigure;

    ++plotDataArrayIdx;

    if (plotDataArrayIdx == [self.plotData count]) {
        [plotStipTimer invalidate];
        plotStipTimer = nil;
    }

}

- (void)configurePLotStrip:(F3PlotStrip *)plotStrip {

    plotDataArrayIdx = 0;
    // Random rubbish that makes it look like a heartbeat :
    self.plotData = [NSArray arrayWithObjects:[NSNumber numberWithFloat:68.1f],
                 [NSNumber numberWithFloat:70.2f],
                 [NSNumber numberWithFloat:71.3f],
                 [NSNumber numberWithFloat:72.4f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:100.5f],
                 [NSNumber numberWithFloat:124.5f],
                 [NSNumber numberWithFloat:10.5f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:70.6f],
                 [NSNumber numberWithFloat:70.2f],
                 [NSNumber numberWithFloat:71.3f],
                 [NSNumber numberWithFloat:72.4f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:100.5f],
                 [NSNumber numberWithFloat:124.5f],
                 [NSNumber numberWithFloat:10.5f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:70.6f],
                 [NSNumber numberWithFloat:70.2f],
                 [NSNumber numberWithFloat:71.3f],
                 [NSNumber numberWithFloat:72.4f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:100.5f],
                 [NSNumber numberWithFloat:124.5f],
                 [NSNumber numberWithFloat:10.5f],
                 [NSNumber numberWithFloat:73.5f],
                 [NSNumber numberWithFloat:70.6f],
                 nil];

    // Get value bounds
    float max = -999999;
    float min = 999999;
    for (NSInteger i = 0;i <= self.plotData.count - 1;++i) {
        float RSEFigure = [[self.plotData objectAtIndex:i] floatValue];
        if (RSEFigure > max)
            max = RSEFigure;
        if (RSEFigure < min)
            min = RSEFigure;
    }

    plotStrip.lowerLimit = min;
    plotStrip.upperLimit = max;
    plotStrip.lineColor = COLOUR_POWDER_BLUE;
    [self.view addSubview:_IBOPLotView];
    plotStipTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(testGraph:) userInfo:nil repeats:YES];
    plotStrip.showDot = YES;
    plotStrip.capacity = self.plotData.count;

}

- (void)viewDidLoad {

    if (bUsingDynPlot) {
        // Dynamically adding to view.
        _plotView2 = [[F3PlotStrip alloc] init];
        _plotView2.backgroundColor = [UIColor redColor];
        _plotView2.frame = CGRectMake(0, 100, 293, 35);
        [[self view] addSubview:_plotView2];
        [self configurePLotStrip:_plotView2];
    } else {
        [self configurePLotStrip:_IBOPLotView];
    }
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end

我希望这对你来说很容易加入一个新的 VC。如果你的项目文件夹中有 F3PlotStrip.h 和 .m,你会很好 - 除了向 VC 添加 UIView。

卡尔。

于 2013-05-16T22:32:04.883 回答