【问题标题】:iOS UI Customization optimizationiOS UI 自定义优化
【发布时间】:2013-01-11 18:07:22
【问题描述】:

我即将开始一个包含大量 UI 自定义的大型项目。 例如,我需要一个看起来像这样的 UIView:

所以我写了一个 UIView 自定义类,但我真的不知道我的代码是否经过优化以及是否有最好的方法来做到这一点,请问您能给我一些建议吗?

这是我的代码:

// DPIViewHeader.h

#define     HEADER_HEIGHT   30.0f
#define     HEADER_COLOR    [UIColor colorWithRed:161.0f/255.0f green:209.0f/255.0f blue:249.0f/255.0f alpha:1.0f]

#define     BORDER_WIDTH    2.0f
#define     BORDER_COLOR    [[UIColor colorWithRed:82.0f/255.0f green:159.0f/255.0f blue:210.0f/255.0f alpha:1.0f] CGColor]

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface DPIViewWithHeaderTest : UIView

// properties
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) UIColor *headerColor;

@property (strong, nonatomic) UIView *headerView;
@property (strong, nonatomic) UIView *contentView;

// methods
- (id)initWithFrame:(CGRect)frame title:(NSString *)title;
- (id)initWithFrame:(CGRect)frame title:(NSString *)title headerColor:(UIColor *)headerColor;

@end

// DPIViewHeader.m

#import "DPIViewWithHeaderTest.h"

@implementation DPIViewWithHeaderTest

- (id)initWithFrame:(CGRect)frame title:(NSString *)title {
    self = [super initWithFrame:frame];
    if (self) {
        self.title = title;
    }
    return self;
}

- (void)layoutSubviews {
    // set header view and content view
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.frame.size.width, HEADER_HEIGHT)];
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.origin.x, self.bounds.origin.y + HEADER_HEIGHT, self.frame.size.width, self.frame.size.height - HEADER_HEIGHT)];

    // add title label
    UILabel *label = [[UILabel alloc] init];
    label.text = self.title;
    [label sizeToFit];
    label.center = CGPointMake(label.frame.size.width/2 + 10, self.headerView.frame.size.height/2);
    label.backgroundColor = [UIColor clearColor];
    [self.headerView addSubview:label];

    // set color header
    self.headerView.backgroundColor = HEADER_COLOR;

    // set border attributes
    self.layer.borderWidth = BORDER_WIDTH;
    self.layer.borderColor = BORDER_COLOR;

    self.headerView.layer.borderWidth = BORDER_WIDTH;
    self.headerView.layer.borderColor = BORDER_COLOR;

    // add subviews
    [self addSubview:self.headerView];
    [self addSubview:self.contentView];
}

@end

【问题讨论】:

    标签: ios optimization uiview customization


    【解决方案1】:

    这真的取决于(就像一切一样:))。 让我告诉你为什么使用两种场景:


    • 视图是不可自定义的:

      • 如果将 DPIViewWithHeaderTest 嵌入到 UITableViewCell 中,那么由于内存使用率高,滚动性能会很糟糕。因此不适合此目的。

      • 下一个场景:只是一个简单的视图,位于某处,具有静态背景和一些数据。还可以,但不是最好的解决方案。

    很好的解决方案

    出于这两个目的,我建议创建图像。预渲染的图像被缓存并留下非常小的内存占用。此外,在这种情况下,您甚至可以创建可拉伸的。这不是很棒吗?


    • 如果 UIView 必须是可定制的(如颜色、大小)怎么办?那么这是唯一的解决方案,但我会考虑根据目的重写实现。

      • 如果会有很多这样的视图,它们会被动画化,例如这是 UITableViewCell 的背景,您应该考虑使用 QuartzCore/Core Graphics 来绘制它们以获得更好的性能。

      • 对于一个(或几个)视图,这个实现就很好:)。

    最后一条建议

    一般来说,除非视图是可定制的,否则我会建议创建图像。 出于三个原因:性能、外观和易于创建。相信我,精心制作的图像看起来比自定义绘图更好:)

    【讨论】:

    • 不难重现样本恕我直言的效果。
    • 非常感谢您的回答!我会像你建议的那样尝试图像方法:)
    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2020-12-12
    • 2012-08-10
    相关资源
    最近更新 更多