私は自分のアプリである化合物に周期表キーボードを実装しました。
UIButton を使用しないでください。
キーボードを表示するには、UIViews または CALayers を使用して個々のキーを表示します。
また
静的 PNG。メモリと「ジッパー」がはるかに簡単です。(これは、まだ来ていない更新のために行ったことです)
親ビューを使用してすべてのタッチを追跡する必要があります。(これがなぜそうであるかを説明するために、下部に小さな脇を追加) touches メソッドを次のように実装します。
- (void)touchesBegan: (NSSet *)touches
withEvent: (UIEvent *)event {
NSLog(@"TouchDown");
CGPoint currentLocation = [[touches anyObject] locationInView:self.view];
[self magnifyKey:[self keyAtPoint:currentLocation]];
}
-(void)touchesMoved: (NSSet *)touches
withEvent: (UIEvent *)event {
NSLog(@"TouchMoved");
CGPoint currentLocation = [[touches anyObject] locationInView:self.view];
[self magnifyKey:[self keyAtPoint:currentLocation]];
}
-(void) touchesEnded: (NSSet *)touches
withEvent: (UIEvent *)event{
NSLog(@"TouchUp");
CGPoint currentLocation = [[touches anyObject] locationInView:self.view];
int key = [self keyAtPoint:currentLocation];
[self selectKey:aKey];
}
これらのメソッドはキーを取得し、選択したキーを適切に「拡大」します。
CGRects の配列に対してポイントを実行して、キーが押されたかどうかを判断します (これは、ヒット テストに比べて高速です)。
- (int)keyAtPoint:(CGPoint)aPoint{
int aKey=1;
for(NSString *aRect in keys){
if(CGRectContainsPoint(CGRectFromString(aRect), aPoint)){
break;
}
aKey++;
}
if(aKey==kKeyOutOfBounds)
aKey=0;
NSLog([NSString stringWithFormat:@"%i",aKey]);
return aKey;
}
- (void)magnifyKey:(int)aKey{
if(aKey!=0) {
if(magnifiedKey==nil){
self.magnifiedKey = [[[MagnifiedKeyController alloc] initWithKey:aKey] autorelease];
[self.view addSubview:magnifiedKey.view];
}else{
[magnifiedKey setKey:aKey];
if([magnifiedKey.view superview]==nil)
[self.view addSubview: magnifiedKey.view];
}
}else{
if(magnifiedKey!=nil)
[magnifiedKey.view removeFromSuperview];
}
}
- (void)selectKey:(int)aKey{
if(magnifiedKey!=nil){
if(aKey!=0){
[magnifiedKey flash];
[self addCharacterWithKey:aKey];
}
[magnifiedKey.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
}
}
実装するメソッドがいくつかありますが、非常に簡単です。基本的に拡大キーであるビューを作成しています。次に、ユーザーが指をスライドさせて移動します。
余談:
タッチがビューによって追跡されると、touchesEnded: (または touchesCancelled:) が呼び出されるまでタッチを追跡し続けるため、サブビューでタッチを追跡することはできません。これは、文字「Q」がタッチを追跡すると、他のキーがそのタッチにアクセスできないことを意味します。文字「W」の上にカーソルを置いていても。これは他の場所で有利に使用できる動作ですが、この状況では、タッチを追跡する役割を持つ「親ビュー」を使用して回避する必要があります。
(メモリリークを修正するために更新)