#include "TouchEx.h"
//TouchExの実装
@implementation TouchEx
//初期化
- (id)initWithFrame:(CGRect)frame {
self=[super initWithFrame:frame];
if (self) {
self.backgroundColor=[UIColor whiteColor];
//タッチオブジェクト群
_touches=[[NSMutableArray array] retain];
//マルチタッチの有効化
self.multipleTouchEnabled=YES;
}
return self;
}
//メモリ解放
- (void)dealloc {
[_touches release];
[super dealloc];
}
//描画
- (void)drawRect:(CGRect)rect {
[[UIColor blackColor] set];
UIFont* font=[UIFont systemFontOfSize:16];
[@"TouchEx>" drawAtPoint:CGPointMake(0,0) withFont:font];
//タッチ位置の表示
for (int i=0;i<_touches.count;i++) {
CGPoint pos=[[_touches objectAtIndex:i] locationInView:self];
NSString* str=[NSString stringWithFormat:
@"touchPos[%d]=(%.1f,%.1f)\n",i,pos.x,pos.y];
[str drawAtPoint:CGPointMake(0,16+16*i) withFont:font];
}
}
//タッチ開始イベントの処理
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
//タッチオブジェクトに追加
NSArray* objects=[touches allObjects];
for (int i=0;i<objects.count;i++) {
[_touches addObject:[objects objectAtIndex:i]];
}
//再描画
[self setNeedsDisplay];
}
//タッチ移動イベントの処理
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
//再描画
[self setNeedsDisplay];
}
//タッチ終了イベントの処理
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
//タッチオブジェクトの削除
NSArray* objects=[touches allObjects];
for (int i=0;i<objects.count;i++) {
NSObject* object=[objects objectAtIndex:i];
if ([_touches containsObject:object]) {
[_touches removeObject:object];
}
}
//再描画
[self setNeedsDisplay];
}
//タッチキャンセルイベントの処理
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
//タッチオブジェクトの削除
[_touches removeAllObjects];
//再描画
[self setNeedsDisplay];
}
@end
|