我有一个 View Controller ,其 View 由由 2 个按钮(上一个和下一个)控制的分页 ScrollView 组成。在此 ScrollView 中有几个自定义 View ,每个 View 的大小为一页。
当我加载主视图时, ScrollView 被设置为包含所有这些 subview 。我的应用程序的性能目前在这方面是 Not Acceptable ,因为加载 View 大约需要 3 秒。我注意到,如果我在自定义 View 上注释掉我的drawrect 方法,性能会显着提高。有人可以看一下这段代码,看看我在做什么,资源如此匮乏吗?我对核心显卡真的很陌生,怀疑我做了一些明显错误的事情。
谢谢
- (void)drawRect:(CGRect)rect {
UILabel *questionNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 200, 50)];
[questionNumberLabel setBackgroundColor:[UIColor clearColor]];
[questionNumberLabel setText:[NSString stringWithFormat:@"Question %i", surveyQuestionNumber]];
[questionNumberLabel setTextAlignment:UITextAlignmentCenter];
[questionNumberLabel setFont:[UIFont boldSystemFontOfSize:28.0]];
[questionNumberLabel setTextColor:[UIColor whiteColor]];
[questionNumberLabel setShadowOffset:CGSizeMake(0, -1.0)];
[questionNumberLabel setShadowColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
[self addSubview:questionNumberLabel];
[questionNumberLabel release];
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
CGColorRef lightGreenColor = [[UIColor colorWithRed:158.0/255.0 green:192.0/255.0 blue:72.0/255.0 alpha:1.0] CGColor];
CGColorRef darkGreenColor = [[UIColor colorWithRed:102.0/255.0 green:142.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
CGColorRef shadowGreenColor = [[UIColor colorWithRed:71.0/255.0 green:100.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
CGFloat outerMargin = 20.0f;
CGRect outerRect = CGRectInset(self.bounds, outerMargin, outerMargin);
CGMutablePathRef outerPath = createRoundedRectForRect(outerRect, 15.0);
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, whiteColor);
CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, shadowColor);
CGContextAddPath(context, outerPath);
CGContextFillPath(context);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextAddPath(context, outerPath);
CGContextClip(context);
drawLinearGradient(context, outerRect, whiteColor, lightGrayColor);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, whiteColor);
CGContextAddPath(context, outerPath);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CGRect ribbonyRect = CGRectMake(10, 40, 220, 50);
CGContextSaveGState(context);
CGMutablePathRef ribbonPath = CGPathCreateMutable();
CGPathMoveToPoint(ribbonPath, NULL, ribbonyRect.origin.x, ribbonyRect.origin.y);
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width, ribbonyRect.origin.y);
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width-20, ribbonyRect.origin.y+((ribbonyRect.size.height)/2));
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width, ribbonyRect.origin.y+ribbonyRect.size.height);
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x, ribbonyRect.origin.y+ribbonyRect.size.height);
CGPathCloseSubpath(ribbonPath);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, shadowColor);
CGContextAddPath(context, ribbonPath);
CGContextFillPath(context);
CGContextRestoreGState(context);
CGContextAddPath(context, ribbonPath);
CGContextClip(context);
drawLinearGradient(context, ribbonyRect, lightGreenColor, darkGreenColor);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextAddPath(context, ribbonPath);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, lightGreenColor);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextAddPath(context, ribbonPath);
CGContextSetLineWidth(context, 1.5);
CGContextSetStrokeColorWithColor(context, darkGreenColor);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CGContextMoveToPoint(context, ribbonyRect.origin.x, ribbonyRect.origin.y+ribbonyRect.size.height+1.5);
CGContextAddLineToPoint(context, 19.0, ribbonyRect.origin.y+ribbonyRect.size.height+1.5);
CGContextAddLineToPoint(context, 19.0, ribbonyRect.origin.y+ribbonyRect.size.height+12.0);
CGContextSetFillColorWithColor(context, shadowGreenColor);
CGContextFillPath(context);
CFRelease(ribbonPath);
CFRelease(outerPath);
questionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 110.0, 568.0, 100.0)];
[questionTitleLabel setBackgroundColor:[UIColor clearColor]];
[questionTitleLabel setText:[currentQuestion questionTitle]];
[questionTitleLabel setFont:[UIFont systemFontOfSize:30.0]];
[questionTitleLabel setTextAlignment:UITextAlignmentCenter];
[questionTitleLabel setTextColor:[UIColor grayColor]];
[questionTitleLabel setShadowColor:[UIColor whiteColor]];
[questionTitleLabel setShadowOffset:CGSizeMake(0, -1.0)];
[questionTitleLabel setLineBreakMode:UILineBreakModeWordWrap];
[questionTitleLabel setNumberOfLines:0];
[self addSubview:questionTitleLabel];
questionHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 215.0, 568.0, 30.0)];
[questionHintLabel setBackgroundColor:[UIColor clearColor]];
[questionHintLabel setText:[currentQuestion questionHint]];
[questionHintLabel setFont:[UIFont italicSystemFontOfSize:15.0]];
[questionHintLabel setTextAlignment:UITextAlignmentCenter];
[questionHintLabel setTextColor:[UIColor grayColor]];
[questionHintLabel setShadowColor:[UIColor whiteColor]];
[questionHintLabel setShadowOffset:CGSizeMake(0, -1.0)];
[self addSubview:questionHintLabel];
}
我已经总结了我最新的代码的要点。如果有人可以看一下here ,我真的很感激
最佳答案
有两件事让我突然想到。
1.) 在其他地方实例化您的 UILabels - 可能在 -viewDidLoad 中。并在 -viewDidLoad 中对 UILabels 进行必要的设置。更新对象需要时间。因此,将 QuestionXxxLabel 对象设置为 ivars。
2.) 按照同样的思路,在其他地方创建这些 CGColorRef。它们可以是静态类变量或 ivars - 但它们应该定义一次且仅一次。
例如,您可以创建一个 color.h 文件,该文件可以包含在可能使用这些颜色的地方。
/*
* colors.h
*/
// use _COLORS_ to insure that colors.h is not included multiple times
// i.e., ANSI standard way of constructing an inclusion guard.
#ifndef _COLORS_
#define _COLORS_
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
CGColorRef lightGreenColor = [[UIColor colorWithRed:158.0/255.0 green:192.0/255.0 blue:72.0/255.0 alpha:1.0] CGColor];
CGColorRef darkGreenColor = [[UIColor colorWithRed:102.0/255.0 green:142.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
CGColorRef shadowGreenColor = [[UIColor colorWithRed:71.0/255.0 green:100.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
#endif // _COLORS_
关于iphone - 加载 ScrollView 的性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4025682/