33

だから私は大きな を持っていますUIButton、それは ですUIButtonTypeCustom、そしてボタンターゲットは のために呼び出されUIControlEventTouchUpInsideます。UIButton私の質問は、タッチが発生した場所をどのように判断できるかです。タッチ位置からポップアップを表示できるように、この情報が必要です。これが私が試したことです:

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

など、さまざまな繰り返し。ボタンのターゲット メソッドは、以下を介してボタンから情報を取得しますsender

    UIButton *button = sender;

それで、次のようなものを使用できる方法はありますbutton.touchUpLocationか?

私はオンラインで見て、これに似たものを見つけることができなかったので、事前に感謝します.

4

2 に答える 2

60
UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

このコードがおそらくビュー コントローラーのアクション内にあることを除けば、それは正しい考えですよね? その場合selfは、ボタンではなくビュー コントローラーを参照します。ボタンへのポインタを に渡す必要があります-locationInView:

View Controllerで試すことができるテスト済みのアクションは次のとおりです。

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
    UIView *button = (UIView *)sender;
    UITouch *touch = [[event touchesForView:button] anyObject];
    CGPoint location = [touch locationInView:button];
    NSLog(@"Location in button: %f, %f", location.x, location.y);
}
于 2011-09-11T01:37:21.240 に答える