【问题标题】:touchesBegan method not called when i tap on UIImageView from multiple UIImageView display same as gallery in mobile当我从与移动设备中的图库相同的多个 UIImageView 显示中点击 UIImageView 时,未调用 touchesBegan 方法
【发布时间】:2014-03-27 09:32:45
【问题描述】:

我想触摸 UIImageView 并使用导航视图在另一个视图中以全尺寸显示该图像。与手机中的画廊相同。 只是以编程方式创建演示。因为我是 iphone 开发的新手。

这是我的代码。

- (void)viewDidLoad
{
    NSLog(@"Enter in viewDidLoad method");

    UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);

    [self.view setUserInteractionEnabled:YES];
    [scroll setUserInteractionEnabled:YES];
    [self.view addSubview:scroll];

    int x=5;
    int y=5;
    int hei=100;
    int wid=100;
    int k=1;
    for (int i=0; i<3; i++) 
    {
        for(int j=0;j<50;j++)
        {
            imageView = [[UIImageView alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
            [imageView setImage:[UIImage imageNamed:@"Motocross.png"]];
            [imageView setUserInteractionEnabled:YES];
            scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
            [imageView setTag:k];
            [scroll addSubview:imageView];                        

            NSLog(@"The tag is == %d",k++);
            NSLog(@" (%d,%d)x == %d",i,j,(x+wid)*i+5);
            NSLog(@" (%d,%d)y == %d",i,j,(y+hei)*j+5);

        } 

    }

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    NSLog(@"Enter in touchesBeganWithEvent method");
    UITouch *touch = [touches anyObject];
    if ([touch view] == imageView)
    {
        NSLog(@"Enter in if condition of touch");
        DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
        detailViewController.imageD = imageView.image;

        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];

        NSLog(@"Exit from if condition of touch");
    }
    NSLog(@"Exit from touchesBeganWithEvent method");
}

我想点击 imageView 并且视图将导航到 detailViewController 并以大尺寸显示该图像。但是当我点击 imageView 时没有调用这个 touchesBegan 方法。在这我不是创建 UIImageView 数组只是在循环中分配 init .. 谢谢!

【问题讨论】:

  • 是否调用了 touchesBegan 方法?您是否尝试在方法中插入断点?
  • @我想你的检查不正确试试这个:if([touch.view isKindOfClass:[UIImageView class]])

标签: ios uiimageview uitouch touchesbegan


【解决方案1】:

与其在视图上使用touchesBegan:,不如考虑为每个图像视图添加UITapGestureRecognizer。当图像视图被点击时(你已经设置了userInteractionEnabled,这很好)然后你可以得到用sender.view点击的视图。

请注意,您为拥有的每个图像视图创建一个 UITapGestureRecognizer 的新实例,但每个手势的目标/动作可以相同。


你当前代码的问题在于这个检查:

if ([touch view] == imageView)

仅根据您添加到滚动视图的最后一个图像视图验证触摸。因此,触摸最后一个应该可以,但其他所有都行不通。

根据@vin 的评论,该问题的正确解决方案是验证所触摸视图的类类型:

if ([[touch view] isKindOfClass:[UIImageView class]]) {

(尽管滚动视图可能有其他图像视图子类,而不仅仅是您添加的子类...)

【讨论】:

  • 谢谢,但我想使用 touchesBegan 方法,因为我已经告诉过我是新手,这是我的演示项目,所以我必须使用我先生想要的那些东西。
  • 您应该使用正确的工具来完成这项工作,那就是附加到目标视图的手势识别器
  • 我认识我的朋友,但我不能推迟这个话题来学习,因为我正在接受培训。
  • 但是我的朋友我什至不能输入这个方法(当我点击时没有调用方法请看问题。)
  • 尝试将 scrollView 的 canCancelContentTouches 设置为 NO 并将 delaysContentTouches 设置为 YES。滚动视图正在吞噬事件(使用手势的另一个原因......)
【解决方案2】:

尝试下面的代码或更好地使用集合视图

    - (void)viewDidLoad
{
    NSLog(@"Enter in viewDidLoad method");

    UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);

    [self.view setUserInteractionEnabled:YES];
    [scroll setUserInteractionEnabled:YES];
    [self.view addSubview:scroll];

    int x=5;
    int y=5;
    int hei=100;
    int wid=100;
    int k=1;
    for (int i=0; i<3; i++)
    {
        for(int j=0;j<50;j++)
        {
            UIButton *buttonForImage=[[UIButton alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
            [buttonForImage setImage:[UIImage imageNamed:@"Motocross.png"] forState:UIControlStateNormal];
            [buttonForImage addTarget:self action:@selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
            [buttonForImage setTag:k];
            [scroll addSubview:buttonForImage];

            NSLog(@"The tag is == %d",k++);
            NSLog(@" (%d,%d)x == %d",i,j,(x+wid)*i+5);
            NSLog(@" (%d,%d)y == %d",i,j,(y+hei)*j+5);

        }

    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)imageButtonPressed:(UIButton*)sender{

    NSLog(@"Enter in if condition of touch");
    DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.imageD = sender.imageView.image;

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

}

【讨论】:

    【解决方案3】:

    您需要使用 UICollectionview 而不是尝试处理行/列、图像触摸等... 好的教程在这里...UICollectionView Tutorial (假设你不支持iOS 6.0以下)

    【讨论】:

    • 请看上面的cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多