2015-10-15 144 views
-1

enter image description here我想添加边框到孔中的切口(在中间和半透明外侧清晰)。我为此使用了PartialTransparentMaskView。切割圆形边框Swift

https://github.com/heigong/PartialTransparentMaskView 

的代码看起来像这样

mapView.clipsToBounds = false 

    let frame = mapView.frame 

    // Add the mask view 
    var array = [CGRect]() 
    //to change the circle customize next line 
    let rect = CGRectMake(frame.origin.x+20,100, frame.width-40, frame.height-300) 

    array.append(rect) 

    let maskColor = UIColor(red: 0.9, green: 0.5, blue: 0.9, alpha: 0.5) 
    let parentView = mapView.superview 
    let pFrame = parentView!.frame 
    let maskView = PartialTransparentMaskView(frame: CGRectMake(0, 0, pFrame.width, pFrame.height), backgroundColor: maskColor, transparentRects: nil, transparentCircles:array, targetView: mapView) 
    parentView!.insertSubview(maskView, aboveSubview: mapView) 

我如何添加一个红框绕了一圈?

回答

0

所以,我检查了PartialTransparentMaskView文件,我看到了添加圆圈的代码。我添加了这些线绘制边界:

CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor); 
      CGContextSetAlpha(context,2); 
      CGContextStrokeEllipseInRect(context, holeRectIntersection); 

代码部分看起来是这样的:

 if let circles = transparentCircles { 
     for aRect in circles { 

      let holeRectIntersection = aRect 

      let context = UIGraphicsGetCurrentContext(); 

      if(CGRectIntersectsRect(holeRectIntersection, rect)) 
      { 
       CGContextAddEllipseInRect(context, holeRectIntersection); 
       CGContextClip(context); 
       CGContextClearRect(context, holeRectIntersection); 
       CGContextSetFillColorWithColor(context, UIColor.clearColor().CGColor) 
       CGContextFillRect(context, holeRectIntersection); 
       CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor); 
       CGContextSetAlpha(context,2); 
       CGContextStrokeEllipseInRect(context, holeRectIntersection); 
      } 


} 
     } 
0

在maskView上方添加一个UIView,它的框架方块等于圆的半径,以圆心为中心。

给出让circleRadius,让circleCentre是你的价值观

... 
circleView.borderColor = UIColor.BlackColor() 
circleView.borderWidth = 1.0 
circleView.backgroundColor = UIColor.clearColor() 
circleView.layer.cornerRadius = circleRadius 
circleView.frame = CGRectMake(circleCentre.x - circleRadius, circleCentre.y - circleRadius, circleRadius * 2, circleRadius * 2) 
parentView!.insertSubview(circleView, aboveSubview: maskView) 

作为示例值,如果没有什么时髦的事情和看法仅仅是在顶部的看法,即应该工作。

+0

我不明白,什么是错的这件事吗?如果你打算把我的答案投下来,请至少让我知道为什么我错了。 –

+0

它是显示边框的Maskview,这是整个框架。我想要它的内部圆形区域。 – subodh1989

+0

我已经更新了我的答案,我最初认为面具视图是圆圈。我很抱歉,这并不明显。这对你有帮助吗? –

0

maskView只是一个视图。它甚至不是一个面具;这只是一个视图,其中包含一些透明度。因此,将红色边界绘制到视图中,就像您将任何东西绘制到视图中一样。

+0

MaskView是父项。如果我为它添加边框,它将围绕整个框架而不是内部圆圈。 – subodh1989

+0

我没有说添加边框。我说绘制红色边界,一个椭圆。 – matt

+0

让我试试这个。 – subodh1989