iOS UI 自定义优化

标签 ios optimization uiview customization

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

enter image description here

所以我写了一个 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

最佳答案

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


  • View 是不可自定义的:

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

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

好的解决方案

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


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

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

    • 对于一个(或几个) View ,此实现就很好:)。

最后一条建议

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

关于iOS UI 自定义优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14284103/

相关文章:

ios - [UIImage imagedNamed :inBundle:compatibleWithTraitCollection:] actually access an image?如何

ios - Firebase - for 循环 Swift 中嵌套的 observeSingleEvent 方法

ios - 优化 iOS 中的多个异步图片下载

ios - 如何通过 Storyboard向 UIButton 添加 subview ?

iphone - 如何为 View 转换的一部分设置动画

ios - UILabel 中不同大小的顶部对齐文本

ios - 未创建 PayLoad 目录

mysql - 优化MYSQL排名mod查询

optimization - 使用 CUDA 进行蒙特卡洛优化

ios - UIView子类重绘UIBezierPath而不重绘整个 View