推送 ViewController 后的触摸事件问题

Posted

技术标签:

【中文标题】推送 ViewController 后的触摸事件问题【英文标题】:Problem with Touch-Events after pushing ViewController 【发布时间】:2010-07-27 10:38:45 【问题描述】:

在我推送我以编程方式创建的 ViewController 之前,所有的触摸事件都会被完美识别。 我认为新的 ViewController 和我的代码之间没有任何联系。

这是我推送 ViewController 的代码:

UIViewController *detail = [[UIViewController alloc] init];
CGRect frame = CGRectMake(0, -44, 320, 411);
UIView *galleryView = [[UIView alloc] initWithFrame:frame];
UIImageView *galleryImageView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
galleryImageView.image = [UIImage imageNamed:@"bg.png"];
[galleryView addSubview:galleryImageView];
detail.view = galleryView;

frame = CGRectMake(0, 0, 320, 367);
int height = (([photo_id count] - 1) / 3);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.contentSize = CGSizeMake(320, (height * 102) + 12);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;

[galleryView addSubview:scrollView];

int z = 0;

for (int x = 0; x < ([photo_id count] / 3); x++) 
    for (int y = 0; y <= 2; y++) 

        frame = CGRectMake((y * 90) + (y * 12) + 18, (x * 90) + (x * 12) + 18, 76, 76);
        AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:frame];
        frame = CGRectMake((y * 90) + (y * 12) + 12, (x * 90) + (x * 12) + 12, 90, 90);
        UIImageView *borderView = [[UIImageView alloc] initWithFrame:frame];
        NSString *flickrURL = [photo_secret objectAtIndex:z];
        NSURL *url = [NSURL URLWithString:flickrURL];
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.opaque = NO;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [borderView setUserInteractionEnabled:YES];
        borderView.tag = z;
        NSLog([NSString stringWithFormat:@"%i", borderView.tag]);
        [imageView loadImageFromURL:url];
        borderView.image = [UIImage imageNamed:@"img_border.png"];
        z++;
        [scrollView addSubview:imageView];
        [scrollView addSubview:borderView];
    



[self.navigationController pushViewController:detail animated:YES];

使用以下代码记录触摸事件:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

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

    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Hinweis" message:[NSString stringWithFormat:@"%d", touch.view.tag] delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [errorAlert show];

    NSLog([NSString stringWithFormat:@"%i", touch.view.tag]);   

【问题讨论】:

【参考方案1】:

我找到了解决方案: 继承 UIScrollView 并添加 touchEventHandling。

【讨论】:

以上是关于推送 ViewController 后的触摸事件问题的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView 的触摸事件

touches* 事件 - SKScene 与 ViewController

将 xib 和 ViewController 复制到新项目后,我的应用程序不会绘制并在触摸事件时崩溃

从其他项目推送 ViewController 时出错

从推送的 viewController 到 rootViewController 的信息

快速检测 Viewcontroller 上的第一次触摸