내 프로젝트에 동일한 유형의 여러보기를 추가 할 수 있도록 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;
}
좋은 물건, 건배 친구. 나는 그것을 줄 것이다! –