![]() |
| 赤い丸の描画 |
プログラム
iPhoneでのタッチやタップをtouchesBeganで検知するで記述したBaseWindow.mに、まずdrawRectを追加しました。色を赤に設定し、描画先を取得して、そこに円を書くという流れになります。
次に、touchesBeganに描画を促すsetNeedsDisplayを記述します。drawRectを直接呼んでもうまく描画できないようです。drawRectはオフスクリーン上に描画しておくメソッドで、setNeedsDisplayはそれを画面上に表示するメソッドのようです。
BaseWindow.m
#import "BaseWindow.h"
@implementation BaseWindow
- (void)drawRect:(CGRect)rect {
[[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] setFill];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillEllipseInRect(context, CGRectMake(location.x - 15, location.y -15, 30, 30));
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
location = [[touches anyObject] locationInView:self];
NSLog(@"touchesBegan: (x, y) = (%.0f, %.0f)", location.x, location.y);
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
location = [[touches anyObject] locationInView:self];
NSLog(@"touchesMoved: (x, y) = (%.0f, %.0f)", location.x, location.y);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
location = [[touches anyObject] locationInView:self];
NSLog(@"touchesEnded: (x, y) = (%.0f, %.0f)", location.x, location.y);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
location = [[touches anyObject] locationInView:self];
NSLog(@"touchesCanceled: (x, y) = (%.0f, %.0f)", location.x, location.y);
}
@end
実行して画面上をタッチすると、上図のように赤丸が描画されました。

0 件のコメント:
コメントを投稿