将click事件添加到图像

时间:2013-04-17 12:56:34

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

我是目标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];
}

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

5 个答案:

答案 0 :(得分:2)

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

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
}

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

答案 1 :(得分:0)

使用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
}

答案 2 :(得分:0)

非常简单且最受欢迎的问题解决方案是在图片上使用自定义UIButton。 然后将选择器设置为您要在单击按钮时调用的方法。确保UIButton&的框架UIImage是一样的。

答案 3 :(得分:0)

在xib文件中添加“round rect button”对象。将“类型”属性设置为“自定义”,将“背景”属性设置为“nameofyourimage.extension”。

将aPressed:方法连接到圆形矩形按钮的修饰内部事件。

答案 4 :(得分:0)

无需将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];
    }
}