0

嗨,我有 5 个图像视图,它们都是导航栏中的子视图。我想从每个图像视图中获取点击计数,所以我认为我必须使用 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-

4

1 回答 1

6

天啊。你写了很多重复的代码。当您需要更改某些内容时,这将是一个可怕的时刻。

只有当 userInteractionEnabled == YES 时才会检测到 imageView 中的触摸;而且您始终可以使用自己的类(UIImageView 的子类)来处理和使用触摸。

于 2010-12-21T11:47:35.520 回答