我正在擺弄BezierPath,並注意到我似乎無法弄清的東西。這是代碼-繪圖bezierPath
UIBezierPath *path = [UIBezierPath new];
CGSize screenDimensions = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
CGPoint firstPoint = CGPointMake(screenDimensions.width/2, screenDimensions.height/2);
CGPoint secondPoint = CGPointMake(screenDimensions.width/2 + 10, screenDimensions.height/2 + 10);
CGPoint thirdPoint = CGPointMake(screenDimensions.width/2 - 10, screenDimensions.height/2 + 10);
[path moveToPoint:firstPoint];
[path addLineToPoint:secondPoint];
[path addLineToPoint:thirdPoint];
[path addLineToPoint:firstPoint];
[path closePath];
CAShapeLayer *tempLayer = [CAShapeLayer new];
[tempLayer setPath:path.CGPath];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(screenDimensions.width/2 - 30, screenDimensions.height/2 - 30, 100, 100)];
// UIView *tempView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[tempView setBackgroundColor:[UIColor blueColor]];
tempView.layer.mask = tempLayer;
[self.view addSubview:tempView];
如果我運行上面的代碼塊,在屏幕上添加的UIView上沒有畫任何東西。但是如果我要評論當前的「tempView」並取消當前評論的分配,那麼它將完美地在屏幕上繪製。任何人都可以指出我在做什麼設置框架或錯誤嗎?
啊謝謝!這解決了這個問題,我不知道CAShapeLayer會使用它應用的視圖邊界! – Rikh
如果沒有,這將是可怕的和令人困惑的。 – rmaddy