2012-04-27 2 views
0

내 프로젝트에 동일한 유형의 여러보기를 추가 할 수 있도록 UIView를 서브 클래 싱했습니다. 뷰는 기본적으로 반경이있는 직사각형이며, 왼쪽에 그림이 있고 오른쪽에 텍스트가 있습니다. 이제 코드가 제대로 작동하지 않는다는 것을 잘못 이해하지 말아라.하지만 나는 항상 자신에게 "이게 방법인가, 아니면 정말로 잘못 될 것인가"라고 묻고있다.맞춤형 UIView를 만드는 방법입니까?

1) 저는 UILabels와 CALayers를 혼합합니다.이게 맞습니까?

2) setUpView가 완료된 방식입니까?

3) 괜찮 으면 이미지 이벤트가 터치 이벤트에 응답하게하는 최선의 방법은 무엇입니까?

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

인터페이스 빌더를 사용하면 더 쉽게 만들 수 있습니다. 내가 원하는 크기로 설정된 UIView로 XIB를 만들 것입니다. 그런 다음 레이블 (nameLabel)을 추가하고 원하는 방식으로 구성하십시오. QuartzCore layers cornerRadius를 사용하여 둥근 모서리를 설정할 수 있습니다. 그런 다음 XIB에서 하위 클래스를 사용하여 UIView의 클래스 유형으로 설정할 수 있습니다. 파일 소유자가 아니라보기. 그런 다음 레이블을 콘센트로 연결하면 수동으로 레이블을 만들 필요가 없습니다. 위 코드 중 일부를 제거하고 기능 및 외관을 유지할 수 있습니다. 필자는 인터페이스 빌더 (Interface Builder)가 가능할 때 작업을하도록 배웠다.

희망이 도움이됩니다.

+0

좋은 물건, 건배 친구. 나는 그것을 줄 것이다! –

관련 문제