【问题标题】:How can I write the actions for image view when touch it in iPhone-OS 4.0在 iPhone-OS 4.0 中触摸时如何编写图像视图的操作
【发布时间】:2010-06-28 11:04:59
【问题描述】:

我想在导航栏中显示图像,而不是带有操作的后退按钮。所以现在我使用了图像视图并在导航栏中显示了图像。现在我想为图像视图编写动作,当触摸或单击图像视图时。是否可以为图像视图编写动作?

我目前正在使用 iPhone OS 4.0。

请参阅my previous question

这是我的代码:

     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,5,40,40)];
     [self.navigationController.navigationBar addSubview:view1];
     view1.backgroundColor = [UIColor clearColor];

     UIImage *img = [UIImage imageNamed:@"Back.png"];
     UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
     [view1 addSubview:imgView];

如何编写图像视图的操作?

【问题讨论】:

    标签: iphone uiimageview touch


    【解决方案1】:

    在 iOS 3.2 之前,nou 需要 UIImageView 的子类并实现触摸处理程序。 或者用自定义类型的 UIButton 替换你的 UIImageView。

    在 iOS 3.2+ 中,UIGestureRecognizer 可以让您高效地完成工作 - 只需将适当的添加到您的视图中即可。

    【讨论】:

      【解决方案2】:

      除非您准备好在未来的操作系统版本中破坏它,否则不要将视图直接添加到导航栏。

      UIButton * b = [UIButton buttonWithType:UIButtonTypeCustom];
      b.image = [UIImage imageNamed:@"Back.png"];
      b.frame = (CGRect){{0,0},{40,40}};
      [b addTarget:self action:@selection(wahatever) forControlEvents:UIControlEventTouchUpInside];
      self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:b] autorelease];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多