2010-12-21 10 views
0

こんにちは 私は5つの画像ビューを持っています。それらはすべて、ナビゲーションバーのサブビューです。私は各画像ビューからタップカウントを取得したいので、私はtouchesBeganメソッドを使用しなければならないと思います。実際には、すべての画像ビューでタッチイベントは検出されません。正確には、touchesBeganはツールバーで検出されました。 階層は以下のとおりです。UIImageViewでtouchesbeganが検出されなかった

のUIViewController - > UIToolbar

   -> UINavigationBar -> UIImageView 

これは私のコードです:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
int indexSegment = _docSegmentedControl.selectedSegmentIndex; 


CGRect frameTouch; 

if (indexSegment == 0) { 
    frameTouch = frame; 
}else if (indexSegment == 1) { 
    frameTouch = frame1; 
}else if (indexSegment == 2) { 
    frameTouch = frame2; 
}else if (indexSegment == 3) { 
    frameTouch = frame3; 
}else if (indexSegment == 4) { 
    frameTouch = frame4; 
} 


UITouch *touch = [touches anyObject]; 

//--image 0-- 
CGPoint gestureStartPoint0 = [touch locationInView:imageView]; 
if (CGRectContainsPoint(frame, gestureStartPoint0)) { 
    [NSObject cancelPreviousPerformRequestsWithTarget:imageView selector:@selector(segmentItemTapped:) object:nil]; 
}else { 
    [NSObject cancelPreviousPerformRequestsWithTarget:imageView selector:@selector(segmentItemTapped:) object:nil]; 
} 

//--image 1-- 
CGPoint gestureStartPoint1 = [touch locationInView:imageView1]; 
if (CGRectContainsPoint(frame1, gestureStartPoint1)) { 
    [NSObject cancelPreviousPerformRequestsWithTarget:imageView1 selector:@selector(segmentItemTapped:) object:nil]; 
}else { 
    NSLog(@"false "); 
} 

//--image 2-- 
CGPoint gestureStartPoint2 = [touch locationInView:imageView2]; 
if (CGRectContainsPoint(frame2, gestureStartPoint2)) { 
    [NSObject cancelPreviousPerformRequestsWithTarget:imageView2 selector:@selector(segmentItemTapped:) object:nil]; 
}else { 
    NSLog(@"false "); 
} 

//--image 3-- 
CGPoint gestureStartPoint3 = [touch locationInView:imageView3]; 
if (CGRectContainsPoint(frame3, gestureStartPoint3)) { 
    [NSObject cancelPreviousPerformRequestsWithTarget:imageView3 selector:@selector(segmentItemTapped:) object:nil]; 
}else { 
    NSLog(@"false "); 
} 

//--image 4-- 
CGPoint gestureStartPoint4 = [touch locationInView:imageView4]; 
if (CGRectContainsPoint(frame4, gestureStartPoint4)) { 
    [NSObject cancelPreviousPerformRequestsWithTarget:imageView4 selector:@selector(segmentItemTapped:) object:nil]; 
}else { 
    NSLog(@"false "); 
} 

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
int indexSegment = _docSegmentedControl.selectedSegmentIndex; 
int counter = [_docsegmentmodels count]; 
NSLog(@"index di touches ended : %d", indexSegment); 


CGRect frameTouch; 

if (indexSegment == 0) { 
    frameTouch = frame; 
}else if (indexSegment == 1) { 
    frameTouch = frame1; 
}else if (indexSegment == 2) { 
    frameTouch = frame2; 
}else if (indexSegment == 3) { 
    frameTouch = frame3; 
}else if (indexSegment == 4) { 
    frameTouch = frame4; 
} 


UITouch *touch = [touches anyObject]; 

//--image 0-- 
CGPoint gestureEndPoint0 = [touch locationInView:imageView]; 
if (CGRectContainsPoint(frame, gestureEndPoint0)) { //mencari bagian yg berimpitan dari posisi tap dengan frame segmen 
    if (counter>0){ 
     if (touch.tapCount <= 2) { 
      [self performSelector:@selector(segmentItemTapped:) withObject:nil]; 
     } 
    }else { 
     [NSObject cancelPreviousPerformRequestsWithTarget:navBar selector:@selector(segmentItemTapped:) object:nil]; 
    } 
}else { 
    NSLog(@"CGRectContaintsPOint0 : %d", CGRectContainsPoint(frame, gestureEndPoint0)); 
} 

//--image 1-- 
CGPoint gestureEndPoint1 = [touch locationInView:imageView1]; 
if (CGRectContainsPoint(frame1, gestureEndPoint1)) { //mencari bagian yg berimpitan dari posisi tap dengan frame segmen 
    if (counter>0){ 
     if (touch.tapCount <= 2) { 
      [self performSelector:@selector(segmentItemTapped:) withObject:nil]; 
     } 
    }else { 
     [NSObject cancelPreviousPerformRequestsWithTarget:navBar selector:@selector(segmentItemTapped:) object:nil]; 
    } 
}else { 
    NSLog(@"CGRectContaintsPOint1 : %d", CGRectContainsPoint(frame1, gestureEndPoint1)); 
} 

//--image 2-- 
CGPoint gestureEndPoint2 = [touch locationInView:imageView2]; 
if (CGRectContainsPoint(frame2, gestureEndPoint2)) { //mencari bagian yg berimpitan dari posisi tap dengan frame segmen 
    if (counter>0){ 
     if (touch.tapCount <= 2) { 
      [self performSelector:@selector(segmentItemTapped:) withObject:nil]; 
     } 
    }else { 
     [NSObject cancelPreviousPerformRequestsWithTarget:navBar selector:@selector(segmentItemTapped:) object:nil]; 
    } 
}else { 
    NSLog(@"CGRectContaintsPOint2 : %d", CGRectContainsPoint(frame2, gestureEndPoint2)); 
} 

//--image 3-- 
CGPoint gestureEndPoint3 = [touch locationInView:imageView3]; 
if (CGRectContainsPoint(frame3, gestureEndPoint3)) { //mencari bagian yg berimpitan dari posisi tap dengan frame segmen 
    if (counter>0){ 
     if (touch.tapCount <= 2) { 
      [self performSelector:@selector(segmentItemTapped:) withObject:nil]; 
     } 
    }else { 
     [NSObject cancelPreviousPerformRequestsWithTarget:navBar selector:@selector(segmentItemTapped:) object:nil]; 
    } 
}else { 
    NSLog(@"CGRectContaintsPOint3 : %d", CGRectContainsPoint(frame3, gestureEndPoint3)); 
} 

//--image 4-- 
CGPoint gestureEndPoint4 = [touch locationInView:imageView4]; 
if (CGRectContainsPoint(frame4, gestureEndPoint4)) { //mencari bagian yg berimpitan dari posisi tap dengan frame segmen 
    if (counter>0){ 
     if (touch.tapCount <= 2) { 
      [self performSelector:@selector(segmentItemTapped:) withObject:nil]; 
     } 
    }else { 
     [NSObject cancelPreviousPerformRequestsWithTarget:navBar selector:@selector(segmentItemTapped:) object:nil]; 
    } 
}else { 
    NSLog(@"CGRectContaintsPOint4 : %d", CGRectContainsPoint(frame4, gestureEndPoint4)); 
} 

}

がとてもあり私はtouchesbeganをimageviewsに接続するために定義する必要がありますか? 助けてください について -リスマ

答えて

6

ああ、男。あなたは重複したコードをたくさん書いています。これはあなたが何かを変更する必要があるときに恐ろしい瞬間になります。

touch in imageViewは、userInteractionEnabled == YESの場合にのみ検出されます。そして、あなたはいつも自分のクラス(UIImageViewの子)を使ってタッチを扱い、操作することができます。

+0

私はUIImageViewサブクラスを作る必要があるのですか? –

関連する問題