[英]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.