我为我的应用程序,化合物实施了周期表键盘。
不要使用UIButtons。
要显示你的键盘,使用: UIViews或CALayers显示各个键
OR
静态PNG。记忆和“zippier”更容易。 (这是我为我的未来更新所做的)
您必须使用父视图跟踪所有触摸。 (小抛开添加在底部来解释为什么会是这样)执行触摸方法,像这样:
- (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”上盘旋。这是一种可以在其他地方使用的行为,但在这种情况下,您必须通过拥有一个“父视图”来处理它,而“父视图”的作用是跟踪触摸。
(更新修复内存泄漏)
你也可以考虑在努力适应一个5等级系统,如http://stackoverflow.com/a/4667025/676822来完成你想要什么 – Lucas 2013-01-22 16:50:40