チュートリアル用にこんな感じで、特定のスポットに穴を空けて表示したい。
利用する側
穴を空ける場所と、穴の大きさを指定して使うようにしたい。
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 初回ヘルプ表示
SpotHelpView *spotHelpView = [[SpotHelpView alloc] initWithFrame:self.view.window.bounds];
[spotHelpView makeHoleAt:CGPointMake(290, 45) radius:30];
[self.view.window addSubview:spotHelpView];
}
ビューの定義
@interface SpotHelpView : UIView
-(void) makeHoleAt:(CGPoint)point radius:(CGFloat)radius;
@end
ビューの実装
@implementation SpotHelpView
-(id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 0.5;
}
return self;
}
-(void) makeHoleAt:(CGPoint)point radius:(CGFloat)radius {
CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.fillRule = kCAFillRuleEvenOdd;
mask.fillColor = [UIColor blackColor].CGColor;
// 画面全体
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.bounds];
// 穴を空ける
[maskPath moveToPoint:point];
[maskPath addArcWithCenter:point radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
mask.path = maskPath.CGPath;
self.layer.mask = mask;
}
@end