简体   繁体   English

如何判断我的iPhone应用程序中是否按下按钮?

[英]How can I tell if a button is being pressed in my iPhone app?

How can you tell when a button is being pressed? 如何按下按钮时怎么知道? Specifically, how can you do something while a button is down? 具体来说,如何在按钮关闭时执行某些操作?

While the screen is touched, there will be touch events. 触摸屏幕时,会有触摸事件。 There are also touch up and touch down events. 还有触摸和触地事件。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
}

Then just register your controller as the touchController . 然后只需将您的控制器注册为touchController

Also check out Apple's GLPaint example: https://developer.apple.com/iphone/library/samplecode/GLPaint/ 另请参阅Apple的GLPaint示例: https//developer.apple.com/iphone/library/samplecode/GLPaint/

You can also do it by creating an IBAction and connecting it IB. 您也可以通过创建IBAction并将其连接到IB来完成。 Interface builder for iPhone can actually demux different types of events. iPhone的界面构建器实际上可以分解不同类型的事件。 If you ctrl-click a UIButton in your UI, and release the mouse button instead of dragging a connection you will see an inspector that shows a bunch of different actions you can wire up. 如果在UI中按住Ctrl键单击UIButton,然后释放鼠标按钮而不是拖动连接,您将看到一个检查器,显示可以连接的一系列不同操作。 When you just drag it defaults to wiring up "Touch Up Inside", but if you want you can wire up "Touch Down" or "Touch Down Repeat" and that action will be cause the appropriate selector to be called. 当你只是拖动它默认接线“Touch Up Inside”,但如果你想要你可以连线“Touch Down”或“Touch Down Repeat”,那么这个动作将导致调用适当的选择器。

If you are doing complicated stuff with your own gestures then using the touchController based stuff will definitely be a better approach, but for simple things just using the built in nib based actions may be simpler. 如果你使用自己的手势做复杂的事情,那么使用基于touchController的东西肯定是一种更好的方法,但对于简单的事情,只使用内置的基于nib的动作可能更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的应用程序检测到按下的主页按钮? - How can I make my app detect the home button being pressed? 如何在iPhone应用程序中触发电源按钮的操作? - How can I trigger the action of power button in my iPhone app? 按下时如何更改iPhone上按钮的默认颜色? - How can I change the default color of the button on iPhone when pressed? iPhone Dev,UINavigationController,如何以编程方式判断何时按下后退按钮 - iPhone Dev, UINavigationController, How to Programmatically Tell when Back Button is Pressed 如何捕获iOS调试器中按下的“停止”按钮? - How can I capture the “Stop” button being pressed in the iOS debugger? 有什么办法可以告诉我按下屏幕的力度吗 - Is there any way at all that I can tell how hard the screen is being pressed 如何判断iPhone上的移动网络是否可用? - How can I tell if my mobile network is available or not on the iPhone? 我如何告诉我的UIView UIViewController中的按钮已被按下? (迅速) - How do i tell my UIView that the button in UIViewController has been pressed? (Swift) 如何告诉 UIViewController 在 UIPopOverController 中按下按钮? - How can you tell an UIViewController that a button is pressed in an UIPopOverController? 我想在我的iPhone应用程序中添加撤消按钮,我该怎么做? - i want to add the undo button in my iphone app, how can i do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM