2012-10-23 3 views
4

레이어의 마스크 속성을 사용하여 이미지에 UIView를 가면 표시하려고합니다. 나는 "그게 쉽다"라고하는 수많은 예를 보았습니다. 그러나, 약간의 조정 후에는 설명 된 결과를 재현 할 수 없습니다. 레이어 마스크 만 설정하면보기가 사라집니다. 여기CALayer 마스크가 올바르게 작동하지 않습니다.

- (void)setMaskImage:(UIImage *)maskImage 
{ 
    _maskImage = maskImage; 

    self.layer.mask.contents = (__bridge id)(_maskImage.CGImage); 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self != nil) { 
     self.layer.mask = [CALayer layer]; 
    } 

    return self; 
} 

- (void)setFrame:(CGRect)frame 
{ 
    [super setFrame:frame]; 

    self.layer.mask.frame = self.layer.bounds; 
} 

을 내가보기 마스크 사용하려고하고있는 이미지입니다 : 여기에 내가 사용하고있는 코드는 http://cl.ly/0a300G2r133V

답변

1

다음 작품은 예상대로.

6

하나의 가능성은 시스템이 실제로 설정 setFrame:를 사용하지 않을거야 것을 당신의 보기의 지오메트리. setCenter:setBounds:을 사용할 수 있습니다. 또 다른 가능성은 시스템이 뷰의 지오메트리를 전혀 설정하지 않고 마스크 레이어를 추가하기 전에 [super initWithFrame:frame] 호출에서 한 번만 설정된다는 것입니다.

어쨌든, 대신 setFrame:을 무시, 당신은 layoutSubviews를 오버라이드 (override) :

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.layer.mask.frame = self.bounds; 
} 
+0

이것은 부분적으로 발생했지만 완전히 문제는 아닙니다. –

0

당신은 화면의 사용자 정의 모양의있는 UIView의 drawRect() 메소드를 오버라이드 (override) 할 수 있습니다. 다음 접근 방식을 사용해보십시오.

//code for drawRect() 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextAddRect(context,ImageFrame); 
CGContextClip(context); 
CGContextClearRect(context,ImageFrame); 
[super drawRect:rect]; 

imageFrame 영역에서보기를 투명하게 만듭니다. 단지 일반의 CALayer가 작동하지 않는 사용하는 이유

- (void)setMaskImage:(UIImage *)maskImage 
{ 
    if (_maskView == nil) { 
     _maskView = [[UIImageView alloc] initWithImage:maskImage]; 
     _maskView.frame = self.bounds; 
     self.layer.mask = _maskView.layer; 
    } else { 
     _maskView.image = maskImage; 
    } 
} 

- (UIImage *)maskImage 
{ 
    return _maskView.image; 
} 

- (void)setBounds:(CGRect)bounds 
{ 
    [super setBounds:bounds]; 

    _maskView.frame = self.bounds; 
} 

잘 모르겠어요,하지만이 신축성 이미지와 잘 작동의 보너스를 추가합니다

관련 문제