2013-01-11 74 views
1

我即將開始一個包含大量UI自定義的大項目。 例如,我需要一個UIView的,這將看起來像:iOS UI自定義優化

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 
+1

看起來很合理。 – ilmiacs

回答

1

這真的取決於(如一切:))。 讓我告訴你爲什麼使用兩種方案:


  • 觀是不可自定義的:

    • 如果DPIViewWithHeaderTest將被嵌入在一個UITableViewCell,那麼好,滾動性能由於高內存使用率會很糟糕。因此對此目的不合適。

    • 下一個場景:只是一個簡單的視圖,某處,有一個靜態背景和一些數據。沒關係,但不是最好的解決方案。

很好的解決

對於這兩個目的,我建議建立圖像。預渲染的圖像被緩存並留下非常小的內存佔用。而且在這種情況下,你甚至可以創建一個可擴展的。這不是很好嗎?


  • 如果什麼一個UIView必須是定製(如顏色,大小)?那麼這是唯一的解決方案,但我會考慮根據目的重寫實現。

    • 如果有將是大量的這種觀點,他們是動畫,例如這是你應該考慮使用QuartzCore /核芯顯卡有更好的表現畫他們的UITableViewCell的背景。

    • 對於一個(或幾個)視圖,這個實現就好:)。

最後一點建議

的一般情況下,除非該視圖是可定製的,我會建議創建圖像。 有三個原因:表現,外觀和創作的易用性。相信我,精心製作的圖像看起來比定製繪圖更好:)

+0

取決於您的自定義繪圖有多好,我的朋友。 – jrturton

+0

從樣本恕我直言重現效果並不難。 – wczekalski

+0

非常感謝您的回答!我會嘗試像你建議我的形象方法:) – Yaman