【问题标题】:Adding click event to image将点击事件添加到图像
【发布时间】:2013-04-10 06:03:19
【问题描述】:

我是目标 c 的新手。我有一张图片,我想在点击它时显示一些警告消息 像这样添加图像-

AHolder = [[UIImageView alloc] initWithFrame:CGRectMake(5, 80, 40, 40)];
    UIImage *imageA = [UIImage imageNamed:@"A.png"];
    AHolder.image = imageA;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aPressed:) name:@"aPressed" object:nil];
    [view addSubview:AHolder]

它的事件是这样的-

 -(IBAction)aPressed:(id)sender
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil];
    [alert show];
    [alert release];
}

它没有给我任何错误,但当我点击图像时也没有任何反应。 请建议我任何解决方案。

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch uiimageview


    【解决方案1】:

    向图像视图添加点击手势。

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
    tapGesture.numberOfTapsRequired=1;
    [AHolder setUserInteractionEnabled:YES];
    [AHolder addGestureRecognizer:tapGesture];
    [tapGesture release];
    
    -(void)handleTapGesture{
    //do what ever you want here
    }
    

    否则,只需使用带有背景图像的按钮。

    【讨论】:

      【解决方案2】:

      使用 tapGestureRecognizer。因此,每当用户点击 iamgeview 时,您都可以做任何您想做的事情

      UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aPressed)];
      tapGesture.numberOfTapsRequired=1;
      [aHolder setUserInteractionEnabled:YES];
      [aHolder addGestureRecognizer:tapGesture];
      
      -(void)aPressed{
      //do what ever you want here
      }
      

      【讨论】:

        【解决方案3】:

        对于您的问题,非常简单且最受欢迎的解决方案是在您的图像上使用自定义 UIButton。 然后只需将选择器设置为您想要在单击按钮时调用的方法。确保 UIButton 和 UIImage 的框架相同。

        【讨论】:

          【解决方案4】:

          在 xib 文件中添加“圆形矩形按钮”对象。将“Type”属性设置为“custom”,将“Background”属性设置为“nameofyourimage.extension”。

          将 aPressed: 方法连接到圆形矩形按钮的 touch up inside 事件。

          【讨论】:

            【解决方案5】:

            不需要将UIButton 放在您的图像上。只需使用UIButton 而不是UIImageView。您可以将UIButton backgroundimageimage 属性设置为您想要的图像。

            除此之外,您还可以像这样使用UITapGestureRecognizer

            将此代码发送至viewDidLoad

            UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
            [yourImage addGestureRecognizer:rec];
            [yourImage setUserInteractionEnabled:YES]; //!! this is important
            

            并实现这个委托方法

            #define unused_related_to_gesture
            - (void)tapRecognized:(UITapGestureRecognizer *)recognizer
            {
                if(recognizer.state == UIGestureRecognizerStateBegan)
                {
                    NSLog(@"\nUIGestureRecognizerStateBegan\n");
                }
                if(recognizer.state == UIGestureRecognizerStateCancelled)
                {
                    NSLog(@"\nUIGestureRecognizerStateCancelled\n");
                }
                if(recognizer.state == UIGestureRecognizerStateChanged)
                {
                    NSLog(@"\nUIGestureRecognizerStateChanged\n");
                }
                if(recognizer.state == UIGestureRecognizerStateEnded)
                {
                    NSLog(@"\nUIGestureRecognizerStateEnded\n");
                }
                if(recognizer.state == UIGestureRecognizerStateFailed)
                {
                    NSLog(@"\nUIGestureRecognizerStateFailed\n");
                }
                if(recognizer.state == UIGestureRecognizerStatePossible)
                {
                    NSLog(@"\nUIGestureRecognizerStatePossible\n");
                }
                if(recognizer.state == UIGestureRecognizerStateRecognized)
                {
                    NSLog(@"\nUIGestureRecognizerStateRecognized\n");
                    // this section will contain your code of clicking the image.
                    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil];
                    [alert show];
                    [alert release];
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-02-11
              • 2014-04-22
              • 2012-04-03
              • 1970-01-01
              相关资源
              最近更新 更多