本文介绍了当UIButton被触发时,你如何获得touchesBegan坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置了touchesBegan方法,用于在用户触摸屏幕时拾取坐标。除非我触摸UIButton,否则它的效果很好。如何使按钮触发touchesBegan方法?
I have a touchesBegan method set up to pick up the coordinates when the user touches the screen. It works great except when I touch down on a UIButton. How do I make it so that the button triggers the touchesBegan method too?
推荐答案
向按钮添加事件处理程序,类似于以下,
Add event handlers to the button, something like the following,
[myButton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
[myButton addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside];
[myButton addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
按照以下方式处理事件,您可以从事件 ev 如下所示,
Handle the events as the following, you can get the touch point from the event ev as below,
- (void)dragBegan:(UIControl *)c withEvent:ev {
NSLog(@"dragBegan......");
UITouch *touch = [[ev allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
}
- (void)dragMoving:(UIControl *)c withEvent:ev {
NSLog(@"dragMoving......");
}
- (void)dragEnded:(UIControl *)c withEvent:ev {
NSLog(@"dragEnded......");
}
这不是我自己的答案。我从并做了一些小的改动。
This is not my own answer. I got this code from http://sree.cc/iphone/handling-touche-events-for-uibuttons-in-iphone and made some minor changes.
这篇关于当UIButton被触发时,你如何获得touchesBegan坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!