2014-04-13 2 views
0

기본적으로 순환 배경을 가진 숫자 인 UIView를 만들려고합니다. UIView를 사용하고 원의 치수 반의 코너 반경을 적용하고 있습니다. 그런 다음 위의보기에 UILabel 하위 뷰로 숫자를 추가합니다.UIView 하위 클래스를 구현할 수 없습니다.

내가 원하는 것은이 (공상 경계선 없음) : (이것은 Count Battle 앱의 스크린 샷)입니다. http://a3.mzstatic.com/us/r30/Purple/v4/ca/ea/a4/caeaa4a0-024d-e810-addb-17a12ea18000/screen568x568.jpeg

올바른 방법 (drawrect, init, layoutSubviews 또는 사용자 지정 방법)으로 코드를 이동하여 다시 작성하도록 도와주세요. 이것은 현재 형태의 코드입니다. 나는이 물건에 대한 나의 이해가 혼란 스럽다고 생각한다. 그리고 이것은 올바르게 보이지 않는다.

// CircleNumView.h 

#import <UIKit/UIKit.h> 

@interface CircleNumView : UIView 

@property (nonatomic, strong) UIColor *circleColor; 

- (instancetype)initWithRadius:(CGFloat)radius 
         center:(CGPoint)center 
          text:(NSString *)text; 

@end 

이 구현 파일입니다 :

헤더 파일입니다

// CircleNumView.m 

#import "CircleNumView.h" 

@interface CircleNumView() 

@property (nonatomic) CGFloat radius; 
@property (nonatomic, strong) NSString *text; 

@end 


@implementation CircleNumView 

// designated initializer 
- (instancetype)initWithRadius:(CGFloat)radius 
         center:(CGPoint)center 
          text:(NSString *)text 
{ 
    self = [super init]; 

    self.radius = radius; 
    self.text = text; 
    self.frame = CGRectMake (center.x - radius, center.y - radius, 2 * radius, 2 * radius); 

    self.circleColor = [UIColor whiteColor]; 

    self = [self createView]; 

    return self; 
} 


- (CircleNumView *)createView 
{ 
    CircleNumView *circularView = [[UIView alloc] initWithFrame:self.frame]; 

    circularView.backgroundColor = self.circleColor; 

    UILabel *label = [[UILabel alloc] initWithFrame:circularView.bounds]; 
    label.text = self.text; 
    label.textColor = [UIColor blackColor]; 

    [circularView addSubview:label]; 

    circularView.clipsToBounds = YES; 
    circularView.layer.cornerRadius = self.radius; 

    [self addSubview:circularView]; 

    return circularView; 
} 

@end 

답변

1

당신은 그 self = [createView];

이 될 때까지 꽤 잘 모든 일을했다가 구현 파일입니다 그 I 다음과 같이 작성하십시오 :

// 
// CircleNumberView.m 
// 
// 
// Created by James Valaitis on 13/04/2014. 
// 
// 

#import "CircleNumberView.h" 

#pragma mark - Circle Number View Private Class Extension 

@interface CircleNumberView() 

/** The radius of this circular view. */ 
@property (nonatomic, assign)  CGFloat    radius; 
/** The number to present encapsulated as a string. */ 
@property (nonatomic, copy)   NSString   *text; 
/** The label that shows the number contents of this view. */ 
@property (nonatomic, strong)  UILabel    *textLabel; 

@end 

#pragma mark - Circle Number View Implementation 

@implementation CircleNumberView 

#pragma mark - Initialisation 

/** 
* Initialises a circlular view with a number in the middle. 
* 
* @param radius     The radius of the circle. 
* @param center     The center point of the circle in it's superview. 
* @param text     The number as a string to present in the middle of this view. 
* 
* @return An initialized object. 
*/ 
- (instancetype)initWithRadius:(CGFloat)radius center:(CGFloat)center text:(NSString *)text 
{ 
    CGRect frame = CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2); 

    if (self = [super initWithFrame:frame]) 
    { 
     _radius = radius; 
     _text = text; 

     [self configureViewAndSubviews]; 
    } 

    return self; 
} 

#pragma mark - Property Accessor Methods - Getters 

/** 
* The label that shows the number contents of this view. 
* 
* @return The label that shows the number contents of this view. 
*/ 
- (UILabel *)textLabel 
{ 
    if (!_textLabel) 
    { 
     _textLabel = [[UILabel alloc] initWithFrame:self.bounds]; 
     _textLabel.numberOfLines = 0; 
     _textLabel.textColor = [UIColor blackColor]; 
    } 

    return _textLabel; 
} 

#pragma mark - Subview Management 

/** 
* Configures this view as well as it's subviews. 
*/ 
- (void)configureViewAndSubviews 
{ 
    self.backgroundColor = [UIColor whiteColor]; 
    self.clipsToBounds = YES; 
    self.layer.cornerRadius = self.radius; 

    self.textLabel.text = self.text; 
    [self addSubview:self.textLabel]; 
} 

@end 
+0

고맙습니다! 그들이 말하는 것처럼 뒤늦은 지경은 20/20입니다. 이제 나는 왜 내가이 길을 처음부터 가지 않았는지 설명 할 수 없다. 그런데, 나는 원형보기가 아닌 사각형보기를 얻고 있습니다. 그리고 (CGFloat) 센터 대신 이니셜 라이저에서 (CGPoint) 센터가됩니다. – Roboris

+0

안녕하세요. 수정 된 수정 사항 및 문법 수정으로 변경했습니다. 지금 시도해보십시오. –

+0

정말 고마워 !! PITA는 한 줄의 코드 일 수 있습니다! LOL 나는 반경을 초기화하는 것을 잊어 버렸음을 알지 못했고, 그 동안에는 수많은 변형을 시도했습니다. 심각한 두통을 치료해 주셔서 다시 한번 감사드립니다! :) – Roboris

관련 문제