タッチしたところにUIViewを表示する方法のメモです。
(iOS 5 で試しています。)
ViewController の touch系 メソッドをつかって、タッチされたところに
UIView を表示するサンプルを作ってみます。
手順
1 NSSetから UITouchを取り出す。
2 UITouch の locationInViewで 座標CGPointを取得。
3 座標を center にもつ、UIView を 表示。
サンプルコード
@implementation ViewController
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// タッチをどれか一つ取り出す。
// anyObjectはNSSetのメソッド。 適当に一つ返してくれる。
UITouch *t = [touches anyObject];
// 画面上の座標を取得
CGPoint point = [t locationInView:self.view];
// タッチされたところにView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
view.center = point;
view.backgroundColor = [UIColor blackColor];
[self.view addSubview:view];
}
@end