【问题标题】:iphone-give transparent background to CAShapeLayer *maskLayeriphone-给 CAShapeLayer *maskLayer 透明背景
【发布时间】:2013-04-12 17:42:15
【问题描述】:

我在UIView 上有一个UIImageView。 我已经使用UIBezierPath 绘制了一个形状。现在我希望其他部分是透明的。 我的代码是,

- (void)drawRect:(CGRect)rect
{
    self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    aPath = [UIBezierPath bezierPath];
    CGContextRef aRef ;
    aRef = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] setStroke];
    [[UIColor clearColor] setFill];
    aPath.lineWidth = 2;
    aPath = [UIBezierPath new];
                //path is calculated here using addLineToPoint addArcWithCentre methods
    [aPath fill];
    [aPath stroke];
    [self setClippingPath:aPath :imgV];
    [[[UIColor clearColor] colorWithAlphaComponent:0.0] setFill];
    CGContextFillRect(aRef, rect);
    CGContextFillPath(aRef);
    self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
}
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{   
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imgView.frame;
    maskLayer.path = [clippingPath CGPath];
    maskLayer.backgroundColor = [[[UIColor clearColor] colorWithAlphaComponent:0.0] CGColor];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];

    [imgView removeFromSuperview];
    imgView.layer.mask = maskLayer;

    [self addSubview:imgView];

}   

输出如下图所示。该图像周围的黑色部分应该是透明的 请帮帮我。

【问题讨论】:

    标签: iphone ios uiview cashapelayer


    【解决方案1】:

    我认为您可能错过了最后一个电话,可能就在 addSubview 之前:

    imgView.clipsToBounds = YES;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 2012-01-20
      • 2021-09-20
      • 2019-02-07
      • 2020-10-15
      相关资源
      最近更新 更多