iPhone 自定义键盘
我正在创建一个自定义键盘,其中 10 个 UIButtons 排列在水平行中。这些按钮横跨屏幕宽度,大小相同,并且必须彼此齐平。
我还想允许用户通过沿着按钮行滑动手指来选择按钮。所选按钮的预览显示在屏幕上的其他位置。当用户沿着行移动手指时,预览会更新。当用户对他们的选择感到满意时,他们会松开手指,确认选择。
明显要尝试的是 UICountrolEventDragExit 或 UIControlEventDragOutside 来删除上一个按钮的操作,以及 UIControlEventTouchUpInside 来激活当前按钮并终止以前的触摸事件。然而,UICountrolEventDragExit 和 UIControlEventDragOutside 仅当用户将鼠标拖离给定按钮足够远时才会激活。由于我的按钮必须彼此齐平,这对我来说太远而且不够好。
建议?
I'm creating a custom keyboard with say 10 UIButtons laid out in a horizontal row. The buttons span the width of the screen, are the same size and must sit flush against each other.
I would also like to allow the user to choose a button by sliding a finger along the row of buttons. A preview of the chosen button is displayed elsewhere on the screen. The preview updates as the user moves their finger along the row. When the user is happy with their choice they release their finger, confirming the selection.
The obvious thing to try is UICountrolEventDragExit or UIControlEventDragOutside to remove the action of the previous button and UIControlEventTouchUpInside to activate the current button and kill previous touch events. However UICountrolEventDragExit and UIControlEventDragOutside are only activated when the user has dragged sufficiently far from the given button. Since my buttons must sit flush against each other this is too far and not good enough for me.
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
禁用用于显示按钮的视图上的用户交互,并通过包含视图跟踪所有触摸。这与苹果键盘代码的工作原理类似。
(或者,您可以直接在单个键盘视图的
drawRect:
中绘制所有按钮,但这在方向更改期间看起来不正确)Disable user interaction on the views used to display the buttons, and track all touches through the containing view. This is similar to how Apple's keyboard code works.
(alternatively, you could draw all of the buttons directly in the
drawRect:
of a single keyboard view, but that won't look proper during orientation changes)如果您想要一个 10 个按钮的“键盘”,我会考虑使用 UISegmentedControl。您可以创建一个包含 10 个段的段,每个段都有自己的密钥,并以编程方式接收选择的密钥,如下所示:
If you want a 10 button 'keypad' I would look at using a UISegmentedControl. You can create one with 10 segments each with its own key and receive the key selected programatically like:
调用
-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
明智地使用显示 10 个 UIImages(不是 UIButtons)的视图也可以正常工作。
Calling
-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
judiciously with a view showing 10 UIImages (not UIButtons) also works fine.