嗨,我想在覆蓋層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];
結果不出所料:
我知道我的解決方案將打破如果我必須同時獲得同樣的東西,但是在視圖的左上角。 我正在尋找的只是提供中心(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];
}
你需要一個方法t o傳遞參數來繪製Rect方法?你在問什麼? –
基本上我的drawRect依賴於rectToBeSurrounded變量。至於創建一個圓形,我必須創建一個正方形。我能夠初始化/填充這個變量在commonInitWithRect – Alok
Ÿ不要你簡單地繪製一個帶bezier路徑的圓形,並根據需要將其移動到疊加層上? –