在UIImageView中没有检测到touchesbegan

时间:2010-12-21 08:25:43

标签: ipad uiimageview uinavigationbar uitoolbar touchesbegan

喜 我有5个图像视图,它们都是导航栏中的子视图。我想从每个imageview获得tapcounts,所以我认为我必须使用touchesBegan方法。但实际上,在每个图像视图中都没有检测到触摸事件。确切地说,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 ?? 请帮帮我 问候 -Risma -

1 个答案:

答案 0 :(得分:6)

哦,伙计。你写了很多重复的代码。当你需要改变一些东西时,这将是一个可怕的时刻。

仅当userInteractionEnabled == YES时才会检测到touchView中的触摸;而且你总是可以使用自己的类(UIImageView的子代)来处理和处理触摸。