iphone - 将鼠标悬停在 iPhone 上 UIButton 的状态上

标签 iphone cocoa-touch uibutton

我成功创建了一个自定义键盘,其功能与 Apple 键盘类似。有一点仍然不同。在苹果的 iPhone 键盘上,用户可以用手指在键盘上滑动,所经过的所有按键都会弹出。在我的键盘上,这种情况不会发生。滑动你的手指只会导致我触摸的第一个按钮弹出,当我距离足够远时,它会返回向下。我目前正在使用此代码作为弹出键:

UIImage *bigLetter = [UIImage imageNamed:@"letter1.png"];
[keyOne setImage:bigLetter forState:UIControlStateHighlighted];

keyOne 是一个 UIButton,当用户“突出显示”或点击键盘上的键时,我会覆盖较大字符的图像。是否有类似的状态,只是“悬停在”上,这样如果用户突出显示 Q 键并将手指滑到 W 键上,W 键就会突出显示?

最佳答案

我为我的应用程序“化合物”实现了元素周期表键盘。

不要使用 UIButton。

要显示键盘,请使用: 用于显示各个键的 UIView 或 CALayers

或者

静态 PNG。它更容易内存并且“更快速”。 (这是我为即将到来的更新所做的)

您必须使用父 View 跟踪所有触摸。 (在底部添加小边来解释为什么会这样)像这样实现触摸方法:

- (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];
}

这些方法获取关键点并适当“放大”所选关键点。

我针对 CGRect 数组运行该点来确定按键(与 HitTest 相比,这更快)。

- (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];
        }
    }

那里有一些方法供您实现,但它非常简单。您基本上是在创建一个放大键的 View 。然后当用户滑动手指时移动它。


旁白:

您无法使用 subview 跟踪触摸,因为一旦 View 跟踪触摸,它就会继续跟踪触摸,直到调用 TouchesEnded:(或 TouchesCancelled:) 为止。这意味着一旦字母“Q”跟踪触摸,其他按键将永远无法访问该触摸。即使您将鼠标悬停在字母“W”上。您可以在其他地方利用这种行为,但在这种情况下,您必须通过“父 View ”来解决它,其工作是跟踪触摸。


(更新以修复内存泄漏)

关于iphone - 将鼠标悬停在 iPhone 上 UIButton 的状态上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1195342/

相关文章:

ios - 如何使用 Xcode 约束将 TextField 居中?

iphone - fillMode 到底做了什么?

iphone - 计算足够的文本以适应现有的 UILabel

ios - 如何检测 UIButton 背景图片的变化?

iphone - 完成的iPhone应用程序:添加新功能。录制系统声音然后可以播放

ios - 在 Swift 中读取 UIApplicationLaunchOptionsURLKey

ios - UIViewController 上的自定义属性,如 splitViewController 或 tabBarController

ios - cellForRowAtIndexPath 只被调用一次

ios - 关于修改 NSMutableArray 的内容

php - iOS - 将数据插入 mySql 数据库的正确方法