2014-11-04 5 views
0

사용자 정의 UIControl 서브 클래스에 몇 가지 CAShapeLayer를 그릴 때 이상한 문제가 있습니다. 확실한 것이지만 24 시간이 지나면 제대로 작동하지 않습니다.CAShapeLayers가 사용자 정의 하위 클래스 (UIControl)에 그려지지 않습니다.

나는 이런 식으로 일을 해요 :

// .h 
@interface IntercomButton : UIControl 

@end 

// .m 
@interface IntercomButton() 
@property (nonatomic) CAShapeLayer *outerCircle, *innerCircle; 
@end 

@implementation IntercomButton 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     _innerCircle = [CAShapeLayer layer]; 
     _outerCircle = [CAShapeLayer layer]; 

     _innerCircle.frame = frame; 
     _outerCircle.frame = frame; 

     [self.layer addSublayer:_innerCircle]; 
     [self.layer addSublayer:_outerCircle]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 

    _innerCircle.frame = self.frame; 
    _innerCircle.strokeColor = NULL; 
    _innerCircle.fillColor = [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor; 
    _innerCircle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.frame, 12, 12)].CGPath; 
    //_innerCircle.rasterizationScale = 2.0 * [UIScreen mainScreen].scale; 
    //_innerCircle.shouldRasterize = YES; 
    //[self.layer addSublayer:_innerCircle]; 

    _outerCircle.frame = self.frame; 
    _outerCircle.lineWidth = 4.0f; 
    _outerCircle.strokeColor = [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor; 
    _outerCircle.fillColor = NULL; 
    _outerCircle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.frame, 6, 6)].CGPath; 
    //_outerCircle.rasterizationScale = 2.0 * [UIScreen mainScreen].scale; 
    //_outerCircle.shouldRasterize = YES; 
    //[self.layer addSublayer:_outerCircle]; 
} 

@end 

다른보기는 다음과 같이 생성되고있는 뷰 (그것이 내가 여전히 중요한 생각하지만,하지 않는 것이하는있는 UITableViewCell에있어 ...)

IntercomButton *intercomButton = [[IntercomButton alloc] initWithFrame:btn.frame]; 
intercomButton.backgroundColor = [UIColor clearColor]; 
[detailView addSubview:intercomButton]; 

배경색을 변경하면 UIControl이 잘 보임을 확인할 수 있습니다.

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor); 
    CGContextFillEllipseInRect (context, CGRectInset(rect, 12, 12)); 
    CGContextFillPath(context); 

    CGContextSetLineWidth(context, 3.0f); 
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor); 
    CGContextStrokeEllipseInRect(context, CGRectInset(rect, 8, 8)); 
    CGContextStrokePath(context); 
} 

문제는 내가 애니메이션을 할 필요가있다, 그래서 CAShapeLayers를 사용해야합니다 : 나는 그릴의 drawRect를 사용하는 경우

또한, 그것은 작동합니다. 는 그리고 내가 그것을 사용의 drawRect :

enter image description here

답변

0

잘 보는 방법이있다 (다음과 같아야 그릴 노력하고있어, 내가 저주 할 것이다, 문제는

CGRectInset(self.frame, X, X) 
일을 함께했다

내가

CGRectInset(self.bounds, X, X) 

뷰 프레임 외부에 위치하기 그래서 그쪽으로 층을하고있다한다.

내가 말했듯이, 그것은 명백한 것으로 끝났다.