在 UIImageView 中未检测到 Touchsbegan

发布于 2024-10-08 06:35:41 字数 5837 浏览 4 评论 0原文

你好 我有 5 个图像视图,它们都是导航栏中的子视图。我想从每个图像视图获取点击计数,所以我想我必须使用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 吗? 请帮助我 问候 -里斯玛-

hi
i have 5 imageviews, all off them are subviews in a navigation bar. i want to get tapcounts from each imageview, so i think i have to use touchesBegan method. but actually, there's no touch event detected in every imageview. precisely, the touchesBegan detected in a toolbar.
the hierarchy are :

UIViewController -> UIToolbar

             -> UINavigationBar -> UIImageView

this is my code :

-(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));
}

}

is there something i have to define to connect the touchesbegan to the imageviews??
help me please
regards
-Risma-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难得心□动 2024-10-15 06:35:41

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

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

Oh, man. You're write a lot of duplicated code. This will be scary moment when you will need to change something.

Touch in imageView will detected only if userInteractionEnabled == YES; And you always can use your own class (child of UIImageView) to handle and work with touches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文