2012-04-27 35 views
0

我已經將子類UIView,所以我可以添加多個相同類型的視圖到我的項目。該視圖基本上是一個帶半徑的矩形,左側是一張圖片,右側是一段文字。現在,請不要誤解我的代碼以及所有的代碼,但是我總是問自己:「是這樣的,還是我弄錯了」。這是創建自定義UIView的方式嗎?

1)我把CALayers和UILabels混合在一起,好嗎?

2)setUpView是它完成的方式。

3)如果這是好的,讓imageLayer對觸摸事件作出反應,我最好的選擇是什麼?

4)我可以只問一個問題。作爲一個兼職的初學者,只有一個基本的應用程序在商店我有點挑剔。我的意思是,我應該讓它工作並開始實施!你怎麼看?

@synthesize dateLabel = _dateLabel; 
@synthesize nameLabel = _nameLabel; 
@synthesize photo = _photo; 
@synthesize roundRect= _roundRect; 
@synthesize imageLayer = _imageLayer; 



-(void)setUpView 
{ 

    //create the white rectangle 
     self.roundRect.frame = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height); 
    self.roundRect.cornerRadius = 15.0; 
    self.roundRect.backgroundColor = [UIColor clearColor].CGColor; 
    self.roundRect.borderColor = [UIColor whiteColor].CGColor; 
    self.roundRect.borderWidth = 2.0; 
    self.roundRect.masksToBounds = YES; 

    //create the photo 
    CGImageRef image = [self.photo CGImage]; 
    self.imageLayer.frame = CGRectMake(0.0, 0.0, self.roundRect.frame.size.width*0.48, self.roundRect.frame.size.height); 
    self.imageLayer.borderColor = [UIColor whiteColor].CGColor; 
    self.imageLayer.borderWidth = 2.0; 
    self.imageLayer.masksToBounds = YES; 
    [self.imageLayer setContents:(__bridge id)image]; 
    [self.imageLayer setContentsGravity:kCAGravityResizeAspectFill]; 
    [self.imageLayer setContentsRect:CGRectMake(0.0,0.0,1.1,1.0)]; 

    //create label 
    self.nameLabel.frame = CGRectMake(self.imageLayer.frame.size.width+5, self.roundRect.frame.size.height*0.05, self.roundRect.frame.size.width*0.48,self.roundRect.frame.size.height*0.35); 
    self.nameLabel.backgroundColor = [UIColor clearColor]; 
    self.nameLabel.textAlignment = UITextAlignmentCenter; 
    self.nameLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters; 
    self.nameLabel.textColor = [UIColor whiteColor]; 
    self.nameLabel.adjustsFontSizeToFitWidth =YES; 

    self.nameLabel.font = [UIFont fontWithName:@"Gill Sans" size:50.0]; 








    [self.roundRect addSublayer:self.imageLayer]; 
    [self.layer addSublayer:self.roundRect]; 
    [self addSubview:self.nameLabel]; 

} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 


} 

-(void)setPhoto:(UIImage *)photo 
{ 
    _photo = photo; 

    [self setUpView]; 

} 

-(void)setNameLabel:(UILabel *)nameLabel 
{ 
    _nameLabel = nameLabel; 

    [self setUpView]; 

} 


- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.roundRect = [[CALayer alloc]init]; 
     self.imageLayer = [[CALayer alloc]init]; 
     self.nameLabel = [[UILabel alloc]init]; 


    [self setUpView]; 
    } 
    return self; 
} 

回答

1

您可以通過使用Interface Builder來簡化操作。我會創建一個UIView設置爲我想要的大小的XIB。然後添加一個標籤(nameLabel)並將其配置爲您喜歡的方式。您可以使用QuartzCore圖層cornerRadius來設置圓角。然後,您可以在XIB中使用您的子類,並將其設置爲UIView的類類型。不是文件所有者,而是視圖。然後你可以將你的標籤作爲出口連接,你不需要手動創建它。你可以刪除上面的一些代碼,仍然有你的功能和外觀。我已經學會了讓界面生成器儘可能地完成工作。

希望這會有所幫助。

+0

偉大的東西,歡呼的隊友。我會給它一個去! – 2012-04-27 21:07:06

相關問題