首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的图形在iOS 8的视图中如此缓慢?

为什么我的图形在iOS 8的视图中如此缓慢?
EN

Stack Overflow用户
提问于 2014-12-15 09:53:26
回答 1查看 104关注 0票数 0

我正在开发一个图表。在我的图形视图控制器中,我使用了带有三个按钮"Growth“、"Dancis”、"Ponderal“的UISegmentView。当用户点击段按钮时,我想显示三个不同的图形。

当我点击"Dancis“按钮来显示一个图形时,会花费大量的时间,我不明白为什么要花这么长时间。

我的代码如下:

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];
    mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Growth", @"Dancis", @"Ponderal Index",nil]];

    mainSegment.frame = CGRectMake(250,80,320,43);
    [self.view addSubview:mainSegment];
    // mainSegment.selectedSegmentIndex = 0;
    [mainSegment setWidth:130 forSegmentAtIndex:2];
    [mainSegment setWidth:95 forSegmentAtIndex:1];
    [mainSegment setWidth:95 forSegmentAtIndex:0];
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:mainSegment];

    growthView = [[UIView alloc]initWithFrame:CGRectMake(0 ,125, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:growthView];

    dancisView = [[UIView alloc]initWithFrame:CGRectMake(0 ,125, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:dancisView];

    ponderalIndexView = [[UIView alloc]initWithFrame:CGRectMake(0 ,125, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:ponderalIndexView];

    // Do any additional setup after loading the view.
}


- (void)mainSegmentControl:(UISegmentedControl *)segment
{
    if(mainSegment.selectedSegmentIndex == 0)
    {
        [dancisView setHidden:YES];
        [ponderalIndexView setHidden:YES];
        [growthView setHidden:NO];
        [self initPlot];
    }
    else if(mainSegment.selectedSegmentIndex == 1)
    {
        [dancisView setHidden:NO];
        [ponderalIndexView setHidden:YES];
        [growthView setHidden:YES];

        [self initPlotForDancis];
    }
    else
    {
        [dancisView setHidden:YES];
        [ponderalIndexView setHidden:NO];
        [growthView setHidden:YES];
        [self initPlotForPonderalIndex];
    }
}

生长图包含两个y轴

代码语言:javascript
复制
-(void)configureHost {
    self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0 ,0, growthView.frame.size.width, growthView.frame.size.height-150)];
    self.hostView.allowPinchScaling = YES;
    [growthView addSubview:self.hostView];
}


-(void)configureGraph {
    // 1 - Create the graph
    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
    [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
    self.hostView.hostedGraph = graph;

    // 2 - Set graph title
    NSString *title = @"Ponderal Index Graph";
    graph.title = title;
    // 3 - Create and set text style
    CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
    titleStyle.color = [CPTColor whiteColor];
    titleStyle.fontName = @"Helvetica-Bold";
    titleStyle.fontSize = 16.0f;
    graph.titleTextStyle = titleStyle;
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
    // 4 - Set padding for plot area
    [graph.plotAreaFrame setPaddingLeft:80.0f];
    [graph.plotAreaFrame setPaddingBottom:80.0f];
    [graph.plotAreaFrame setPaddingRight:80.0f];
    // [graph.plotAreaFrame setPaddingBottom:50.0f];

    // 5 - Enable user interactions for plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate = self;

    self.hostView.allowPinchScaling = YES;

}

-(void)configurePlots {

    // 1 - Get graph and plot space
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    // 2 - Create the three plots
    CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init];
    aaplPlot.dataSource = self;
    [plotSpace setDelegate:self];
    aaplPlot.identifier = CPDTickerSymbolAAPL;
    CPTColor *aaplColor = [CPTColor redColor];
    [graph addPlot:aaplPlot toPlotSpace:plotSpace];
    CPTScatterPlot *googPlot = [[CPTScatterPlot alloc] init];
    googPlot.dataSource = self;
    googPlot.identifier = CPDTickerSymbolGOOG;
    CPTColor *googColor = [CPTColor greenColor];
    [graph addPlot:googPlot toPlotSpace:plotSpace];
    CPTScatterPlot *msftPlot = [[CPTScatterPlot alloc] init];
    msftPlot.dataSource = self;
    msftPlot.identifier = CPDTickerSymbolMSFT;
    CPTColor *msftColor = [CPTColor blueColor];
    [graph addPlot:msftPlot toPlotSpace:plotSpace];
    // 3 - Set up plot space
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:aaplPlot, googPlot, msftPlot, nil]];
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(3.0f)];
    plotSpace.xRange = xRange;
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
    plotSpace.yRange = yRange;

    plotSpace.delegate = self;

    // 4 - Create styles and symbols
    CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
    aaplLineStyle.lineWidth = 2.5;
    aaplLineStyle.lineColor = aaplColor;
    aaplPlot.dataLineStyle = aaplLineStyle;
    CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    aaplSymbolLineStyle.lineColor = aaplColor;
    CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
    aaplSymbol.lineStyle = aaplSymbolLineStyle;
    aaplSymbol.size = CGSizeMake(6.0f, 6.0f);
    aaplPlot.plotSymbol = aaplSymbol;
    CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
    googLineStyle.lineWidth = 1.0;
    googLineStyle.lineColor = googColor;
    googPlot.dataLineStyle = googLineStyle;
    CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    googSymbolLineStyle.lineColor = googColor;
    CPTPlotSymbol *googSymbol = [CPTPlotSymbol starPlotSymbol];
    googSymbol.fill = [CPTFill fillWithColor:googColor];
    googSymbol.lineStyle = googSymbolLineStyle;
    googSymbol.size = CGSizeMake(6.0f, 6.0f);
    googPlot.plotSymbol = googSymbol;
    CPTMutableLineStyle *msftLineStyle = [msftPlot.dataLineStyle mutableCopy];
    msftLineStyle.lineWidth = 2.0;
    msftLineStyle.lineColor = msftColor;
    msftPlot.dataLineStyle = msftLineStyle;
    CPTMutableLineStyle *msftSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    msftSymbolLineStyle.lineColor = msftColor;
    CPTPlotSymbol *msftSymbol = [CPTPlotSymbol diamondPlotSymbol];
    msftSymbol.fill = [CPTFill fillWithColor:msftColor];
    msftSymbol.lineStyle = msftSymbolLineStyle;
    msftSymbol.size = CGSizeMake(6.0f, 6.0f);
    msftPlot.plotSymbol = msftSymbol;

}

-(void)configureAxes {
    // 1 - Create styles
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [CPTColor whiteColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor whiteColor];
    axisTextStyle.fontName = @"Helvetica-Bold";
    axisTextStyle.fontSize = 11.0f;
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor whiteColor];
    tickLineStyle.lineWidth = 2.0f;
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;
    // 2 - Get axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
    // 3 - Configure x-axis
    CPTXYAxis *x = axisSet.xAxis;
    x.title = @"GA in Week";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 25.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.tickDirection = CPTSignNegative;
    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:15.0];

    NSInteger majorIncrementx = 5;
    NSInteger minorIncrementx =1;
    CGFloat xMax = 50000.0f;  // should determine dynamically based on max price
    NSMutableSet *xLabels = [NSMutableSet set];
    NSMutableSet *xMajorLocations = [NSMutableSet set];
    NSMutableSet *xMinorLocations = [NSMutableSet set];

    // NSInteger m = 0;
    for (NSInteger j = minorIncrementx; j <= xMax; j += minorIncrementx) {
        NSUInteger mod = j % majorIncrementx;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:x.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j-10);
            // CGFloat location = m+5;
            label.tickLocation = location;
            label.offset = -x.majorTickLength+10 - x.labelOffset+10;
            if (label) {
                [xLabels addObject:label];
            }
            [xMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [xMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    x.axisLabels = xLabels;
    x.majorTickLocations = xMajorLocations;
    x.minorTickLocations = xMinorLocations;


    // 4 - Configure y-axis
    CPTXYAxis *y = axisSet.yAxis;
    y.title = @"PI %";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = 30.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = -10.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.majorTickLength = 4.0f;
    y.minorTickLength = 2.0f;
    y.tickDirection = CPTSignNegative;
    y.axisConstraints = [CPTConstraints constraintWithLowerOffset:10.0];

    NSInteger majorIncrement = 1;
    NSInteger minorIncrement =1;
    CGFloat yMax = 50000.0f;  // should determine dynamically based on max price
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];

    for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            // NSDecimal location = CPTDecimalFromInteger(j);
            NSDecimal location = CPTDecimalFromInteger(j*30);

            label.tickLocation = location;
            label.offset = -y.majorTickLength - y.labelOffset;
            if (label) {
                [yLabels addObject:label];
            }
            [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;

    CPTGraph *graph = self.hostView.hostedGraph;
    //CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;


    CPTXYPlotSpace *plotSpace2 =(CPTXYPlotSpace *) graph.defaultPlotSpace;
    //[[CPTXYPlotSpace alloc] init];
    plotSpace2.delegate = self;
    plotSpace2.allowsUserInteraction = YES;


    [graph addPlotSpace:plotSpace2];

    CPTXYAxis *y2 = [[CPTXYAxis alloc] init] ;
    // 4 - Configure y-axis
    y2.title = @"lenght ";
    y2.titleTextStyle = axisTitleStyle;
    y2.titleOffset = 10.0f;
    y2.axisLineStyle = axisLineStyle;
    y2.majorGridLineStyle = gridLineStyle;
    y2.labelingPolicy = CPTAxisLabelingPolicyNone;
    y2.labelTextStyle = axisTextStyle;
    y2.labelOffset = 50.0f;
    y2.majorTickLineStyle = axisLineStyle;
    y2.majorTickLength = 4.0f;
    y2.minorTickLength = 2.0f;
    y2.tickDirection = CPTSignNegative;
    y2.coordinate = CPTCoordinateY;
    y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:20.0];
    y2.coordinate = CPTCoordinateY;
    y2.plotSpace = plotSpace2;

    NSInteger majorIncrement2 = 100;
    NSInteger minorIncrement2 = 50;
    CGFloat y2Max = 50000.0f;  // should determine dynamically based on max price
    NSMutableSet *y2Labels = [NSMutableSet set];

    NSMutableSet *y2MajorLocations = [NSMutableSet set];
    NSMutableSet *y2MinorLocations = [NSMutableSet set];

    for (NSInteger j = minorIncrement2; j <= y2Max; j += minorIncrement2) {
        NSUInteger mod = j % majorIncrement2;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            // NSDecimal location = CPTDecimalFromInteger(j*30);

            label.tickLocation = location;
            label.offset = -y2.majorTickLength - y2.labelOffset;
            if (label) {
                [y2Labels addObject:label];
            }
            [y2MajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [y2MinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }

    axisSet.axes = [NSArray arrayWithObjects:x, y,y2, nil];

    y2.axisLabels = y2Labels;
    y2.majorTickLocations = y2MajorLocations;
    y2.minorTickLocations = y2MinorLocations;
}

-(NSNumber*) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:      (NSUInteger)idx {

    babyWeight = [[NSMutableArray alloc]init];

    [babyWeight insertObject:@"150" atIndex:0];
    [babyWeight insertObject:@"200" atIndex:1];
    [babyWeight insertObject:@"800" atIndex:2];
    [babyWeight insertObject:@"1000" atIndex:3];
    [babyWeight insertObject:@"1500" atIndex:4];


    switch ( fieldEnum ) {
        case CPTBarPlotFieldBarLocation:
            return @(idx);
            break;

        case CPTBarPlotFieldBarTip:
            return [babyWeight objectAtIndex:idx];
            break;

        default:
            break;
    }

    return nil;
}

- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space
      willChangePlotRangeTo:(CPTPlotRange *)newRange
              forCoordinate:(CPTCoordinate)coordinate
{
    if (coordinate == CPTCoordinateY) {
        newRange = ((CPTXYPlotSpace*)space).yRange;

       // return newRange;
    }
     return newRange;
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-16 00:42:38

您正在创建100,000多个轴标签。这需要时间,而且会给应用程序带来很大的内存压力。选择主要的和次要的增量,以便应用程序只为每个轴生成合理数量的标签。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27481334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档