2015-10-15 146 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