0

UIViewをサブクラス化したので、同じタイプの複数のビューをプロジェクトに追加できます。ビューは基本的に、半径、左側に画像、右側にテキストが表示された長方形です。今、私を誤解しないでください。以下のコードは機能しますが、私はいつも自分自身に「これはこの方法なのか、それとも本当に間違っているのか」と自問しています。

1)CALayersとUILabelsを混在させていますが、これで問題ありませんか?

2)setUpViewはその方法ですか。

3)これで問題がない場合、imageLayerをタッチイベントに応答させるための最善の策は何ですか?

4)サイド質問もお願いします。ストアに基本的なアプリが1つしかないパートタイムの初心者として、私は少しうるさいです。私が言いたいのは、私はそれを機能させてクラックするべきだということです!どう思いますか?

@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;
}
4

1 に答える 1

1

Interface Builder を使用すると、これを簡単に行うことができます。UIView を必要なサイズに設定して XIB を作成します。次に、ラベル (nameLabel) を追加して、好きなように構成します。QuartzCore レイヤーの cornerRadius を使用して、角を丸く設定できます。次に、XIB でサブクラスを使用し、それを UIView のクラス タイプとして設定できます。ファイルの所有者ではなく、ビューです。その後、ラベルをアウトレットとしてリンクでき、手動で作成する必要はありません。上記のコードの一部を削除しても、機能と外観は維持できます。可能な場合は、Interface Builder に作業を任せることを学びました。

お役に立てれば。

于 2012-04-27T20:15:39.807 に答える