2015-11-25 96 views
2

嗨,我想在覆蓋層UIView的右下角創建四分之一透明洞。在覆蓋層上創建右側底部的透明洞UIView

我能夠使用下面的代碼解決它。但它看起來不正確,因爲我在視角之外創建了一個矩形。

我曾嘗試:

@implementation PartialTransparentView 

- (id)initWithBottomRightCornerRadiusForView:(UIView *)view withRadius:(CGFloat)radius 
{ 
[self commonInitWithRect:CGRectMake(view.frame.size.width - radius, view.frame.size.height - radius, radius*2, radius*2)]; 
self = [super initWithFrame:CGRectMake(0, 0, 5000, 5000)];//**it does not look right to me** 
if (self) { 
    // Initialization code 
    self.opaque = NO; 
} 
return self; 
} 

-(void)commonInitWithRect:(CGRect)rect{ 
    backgroundColor = [UIColor colorWithWhite:1 alpha:0.75]; 
    rectToBeSurrounded = rect; 
} 
- (void)drawRect:(CGRect)rect { 

    [backgroundColor setFill]; 
    UIRectFill(rect); 



     CGFloat x = rectToBeSurrounded.origin.x; 
     CGFloat y = rectToBeSurrounded.origin.y; 

     CGFloat width = rectToBeSurrounded.size.width; 
     CGFloat height = rectToBeSurrounded.size.height; 

     //create outer square 
     CGFloat outerX = (x - width/2); 
     CGFloat outerY = y - height/2; 
     CGFloat outerWidth = 2*width; 
     CGFloat outerHeight = outerWidth; 
     //create outer square 

     CGRect outerRect = CGRectMake(outerX, outerY, outerWidth, outerHeight); 

     CGRect holeRectIntersection = CGRectIntersection(outerRect, rect); 

     CGContextRef context = UIGraphicsGetCurrentContext(); 

     if(CGRectIntersectsRect(holeRectIntersection, rect)) 
     { 
      CGContextAddEllipseInRect(context, holeRectIntersection); 
      CGContextClip(context); 
      CGContextClearRect(context, holeRectIntersection); 
      CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); 
      CGContextFillRect(context, holeRectIntersection); 
     } 
} 

我現在用上面的代碼爲:

PartialTransparentView *transparentView = [[PartialTransparentView alloc] initWithBottomRightCornerRadiusForView:self.view withRadius:50]; 
[self.view addSubview:transparentView]; 

結果不出所料:

OUTPUT

我知道我的解決方案將打破如果我必須同時獲得同樣的東西,但是在視圖的左上角。 我正在尋找的只是提供中心(x,y)和半徑以獲得理想的結果。

感謝 BASD上Mr.T

UIView *transparentView = [[UIView alloc] initWithFrame:self.view.frame]; 
    [transparentView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.75]]; 
    [self.view addSubview:transparentView]; 

    circleView *acircleView = [[circleView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)]; 
    [acircleView setBackgroundColor:[UIColor grayColor]]; 
    [transparentView addSubview:acircleView]; 

和circleView.m

- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    //// Oval Drawing 
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(50, 50, 60, 60)]; 
    [UIColor.grayColor setFill]; 
    [ovalPath fill]; 
} 

輸出: dont get circle , instead i am getting square

+1

你需要一個方法t o傳遞參數來繪製Rect方法?你在問什麼? –

+0

基本上我的drawRect依賴於rectToBeSurrounded變量。至於創建一個圓形,我必須創建一個正方形。我能夠初始化/填充這個變量在commonInitWithRect – Alok

+1

Ÿ不要你簡單地繪製一個帶bezier路徑的圓形,並根據需要將其移動到疊加層上? –

回答

1

我的建議是將透明view添加爲單獨view在您的視圖控制器上。這可以在storyboard上完成,以便您可以設置背景顏色和alpha值以給出透明效果!

現在創建另一個view製作圓形並將其添加到透明view,並根據您的需要將此view移動到透明view

創建使用bezier path圓:

circleView.m

- (void)drawRect:(CGRect)frame { 
    //// Oval Drawing 
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), 60, 60)]; 
    [UIColor.grayColor setFill]; 
    [ovalPath fill]; 
} 

出於測試目的,我創造了我的IB一個圓圈視圖和我的視圖控制器創建了一個出口屬性。

HEre是屏幕截圖。

enter image description here

現在移動的圈子,我可以簡單地更改圓view的框架,無論我所需要的。

例如,如果我想將它移動到左上角,我只是做:

-(void)moveCircleViewwithX:(float) x withY:(float) y{ 

    _cView.frame=CGRectMake(x, y, _cView.frame.size.width, _cView.frame.size.height); 


} 

結果將是:

enter image description here

更新

認沽以下透明視圖的drawRect方法:

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGRect transparentPart = self.seeRect;   //this is the rect of the circle view 
CGContextAddEllipseInRect(ctx,transparentPart); //make the circle shape 
CGContextClip(ctx); 
CGContextClearRect(ctx, transparentPart); 

,並在您的視圖控制器:

當你要敷面膜即透明的兩個圓圈和透明層

-(void)applyMask{ 

    [_cView setCircleColor:[UIColor clearColor]]; //circle view bezier path color 
    [_cView setNeedsDisplay]; 
    [_tView setSeeRect:_cView.frame]; //set the transparency cut on transparency view 
    [_tView setNeedsDisplay]; 


} 

一旦你這樣做,你會得到透明度視圖!

您可以通過簡單地調用

 [self moveCircleViewwithX:-30 withY:10]; //top left corner 

移動圈子,你可以通過簡單地調用應用透明面膜:

 [self applyMask]; 

所以,最後的結果你打電話後applyMask方法會:

enter image description here

+0

請看看我的問題,我越來越廣場。感謝您的合作 – Alok

+1

確保您的插座尺寸?它應該是60x60 –

+1

如果你想增加圓的大小,也可以在繪製矩形方法中增加它 –