gpt4 book ai didi

iphone - 加载 ScrollView 性能不佳

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:21:12 24 4
gpt4 key购买 nike

我有一个 View Controller ,其 View 由一个由 2 个按钮(上一个和下一个)控制的分页 ScrollView 组成。在这个 ScrollView 中有几个自定义 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.) 按照相同的思路,在其他地方创建那些 CGColorRefs。它们可以是静态类变量或 ivars - 但它们应该只定义一次。

例如,您可以创建一个 colors.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com