1

多くの 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
4

1 に答える 1

1

それは本当に依存します(すべてのように:))。2 つのシナリオを使用する理由を説明します。


  • ビューはカスタマイズできません:

    • DPIViewWithHeaderTest を UITableViewCell に埋め込む場合、メモリ使用量が多いため、スクロールのパフォーマンスが低下します。したがって、この目的には不適切です。

    • 次のシナリオ: 静的な背景といくつかのデータを備えた単純なビュー。それは問題ありませんが、最善の解決策ではありません。

良い解決策

どちらの目的でも、画像を作成することをお勧めします。事前にレンダリングされたイメージはキャッシュされ、メモリ フットプリントが非常に小さくなります。さらに、この場合、伸縮可能なものを作成することもできます。これは素晴らしいことではありませんか?


  • UIView をカスタマイズ可能 (色、サイズなど) にする必要がある場合はどうなりますか? これが唯一の解決策ですが、目的に応じて実装を書き直すことを検討します。

    • そのようなビューがたくさんある場合、それらはアニメーション化されます。たとえば、これは UITableViewCell の背景であり、パフォーマンスを向上させるために QuartzCore/Core Graphics を使用して描画することを検討する必要があります。

    • 1 つ (または少数) のビューの場合、この実装は問題ありません :) 。

最後のアドバイス

一般に、ビューをカスタマイズ可能にする場合を除き、画像を作成することをお勧めします。3 つの理由: パフォーマンス、外観、および作成の容易さ。私を信じてください、巧妙に作成された画像は、カスタム描画よりもはるかに優れています:)

于 2013-01-11T18:40:31.547 に答える