代码之家  ›  专栏  ›  技术社区  ›  Pugalmuni Tom Tharakan

How can I write the actions for image view when touch it in iPhone-OS 4.0

  •  0
  • Pugalmuni Tom Tharakan  · 技术社区  · 14 年前

    I want to display the image in navigation bar instead of a back button with an action. So now I have used image view and displayed the image in the navigation bar. Now I want to write the actions for image view, when touch or click the image view. Is it possible to write an action for image view?

    I am currently working in 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];
    

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

    2 回复  |  直到 12 年前
        1
  •  2
  •   Eiko    14 年前

    Before iOS 3.2, nou would need a sublass of UIImageView and implement the touch handlers. Or replace your UIImageView with a UIButton with custom type.

    用iOS 3.2 +, UIGestureRecognizer is what gets your things done efficiently - just add the appropriate one to your view.

        2
  •  0
  •   tc.    14 年前

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

    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];