16

これは非常によくある質問であることは承知していますが、すべての Web サイトのすべての回答が機能するとは限りません。それでも意味がわからない場合は、このコード行が理解に役立つかもしれません。


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];
    if (touch.view == nextbutton)
        [self performSelector:@selector(next)];
    if (touch.view == prevbutton)
        [self performSelector:@selector(previous)];
    if (touch.view == moreoptionsbutton)
        [self performSelector:@selector(moresettings)];
}

nextbutton, prevbutton, and more optionsbuttonちなみに、 に触れても何もしませんUIImageViewsisEqual:の代わりにも使用してみ==ましたが、うまくいきませんでした。助言がありますか?

4

2 に答える 2

35

すべての UIImageView に対して userinteractionEnabled = YES を設定する必要があります。そうしないと、タッチ イベントを受け取りません。次の行も変更します。

 UITouch *touch = [[event allTouches] anyObject];

 UITouch *touch = [touches anyObject];
于 2010-04-15T21:08:19.747 に答える
2

続行する前に、クリックされると予想されるビューであることを確認するためのチェックを作成しました。

if( [touch.view isKindOfClass:[Custom class]] ){
    CGPoint touchPoint = [touch locationInView:self.view];

    if( CGRectContainsPoint( self.customClassOne.frame, touchPoint )
       || CGRectContainsPoint( self.customClassTwo.frame, touchPoint ) ){
        [self touchOnCustomClass];
    }
}
于 2014-12-15T13:52:04.423 に答える