2013-04-03 46 views
2

我目前正在研究一个基于图形的iOS应用程序。为此,我使用名为F3PlotStrip的第三方控件。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; 
} 

但我得到的结果是这样的:

Code F3

然后我在xib增加了UIView并改变了它的类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的错误?

回答

1

我今天早上开始使用这个小部件。我现在在我的iPhone上睡觉,所以不能发布我想要发布的内容,但是当我在早上上班时,我会。

我已经设法使用它,并做你想要做什么动态添加UIView,或者我应该说,F3PlotStrip。

我也做了.h和.m,ARC兼容。不知道你是否做到了。

明天我会用一些很好的代码来完成,它使用NSTimer读取NSArray的内容以动态绘制动画。再次以视图添加到我的viewcontroller作为子视图。

没有详细看过你的代码,发布了我明天工作的内容。

好吧 - 我在这里工作,坐在Xcode的前面。这是我用来作为这个小部件的基本测试平台的VC。我昨晚很快就把它撞倒了,但是完成了这项工作。最初,我已经将一个UIView添加到了我的Storyboard的第一个VC中,并将其分类到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 

我希望这很容易让你投入到一个新的风险投资。假如你在你的项目文件夹中有F3PlotStrip.h和.m,那么除了添加一个UIView到VC之外,你还是很好。

Carl。

+0

为什么要在'configurePLotStrip'方法中每次添加'[self.view addSubview:_IBOPLotView];'' –

+0

是的。这似乎有点毫无意义。我基本上需要一个Sparkline类型的绘图仪。我在GitHub上找到了该对象,并用Google搜索了解是否有任何问题。那是我找到这个页面的时候。本章不能以编程方式添加PlotView。无论如何,它不能正确工作。我不在我的MAC前面,在我的手机上偷拍。但我想addSubview是从动态添加方面留下的剩余线。 –

+0

Oooops。道歉。你是那个人!刚注意到。它有什么用途吗?很高兴提供帮助,如果有的话。 –