0

ユーザーが多くのオブジェクトと対話できるアプリがあります。これらはいくつかUIButtons、いくつか、UILabelsそしてたくさんありUIImageViewsます。

すべてのインタラクションの焦点は、オブジェクトに触れることに集中しUIImageViewます。タッチするだけで、画像を移動したり、あれやこれやと指示したりできます。ただし、私の現在の障害は、アプリにタッチしたときに発生するタッチを正しく区別する方法を知ることですUIButton

なんで?Touches Began イベント内のロジックは のみを対象としてUIImageViewsいますが、ボタンやその他のオブジェクトに触れた瞬間、アプリはタッチをUIImageViewオブジェクトに対して発生したかのように解釈します。

つまり、私のアプローチは次のようになります:オブジェクトUIButtonに対してタッチが発生したかどうかを識別する良い方法はUIImageViewありますか? UILabelそうすれば、アプリ内の無関係なタッチを関連するものから除外できます。

編集

以下のコードは、タッチ イベントをキャプチャする方法の概要を示していますが、タッチしたのがボタンなのかビューなのかをタッチ イベントから知る方法がわかりません。

touch = [touches anyObject];
touchLocation = [touch locationInView:[self view]];
4

3 に答える 3

1

この方法を使用できます:-

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
      //You clicked inside the object
    }
    return [super hitTest:point withEvent:event]
}

そしてワインはすでにあなたにそれについての説明を与えました..

https://stackoverflow.com/a/18051856/1865424

于 2013-08-05T07:35:21.590 に答える
1

UIButton が押されているかどうかを知るには、次のようにします。

-(void) touchBegan : (NSSet *) touches withEvent : (UIEvent *) even
{

 UITouch *touch = [touched anyObject];
 UIView *touchedView = [touch view];

 if([touchedView isMemberofClass : [UIButton class])
 {
   //do something when button is touched
 }
}
于 2013-08-28T10:05:18.360 に答える
1

Call hitTest:withEvent: on the view with the touch event to get the view that is actually being touched. Then you can use isKindOfClass: to check what type of view it is and respond accordingly.

于 2013-08-05T06:31:09.600 に答える