如何在Xcode 5中制作隐形按钮?

时间:2014-03-07 17:38:46

标签: ios uiimageview xcode5

我正在尝试制作一个问答游戏。我将xib视图转换为图像视图以放置我的一个预先制作的问题图像。放置图像后,我选择一个按钮并将其放在图像上。但是,该按钮完全取代图像。即使我将按钮类型设置为自定义,我也看不到图像。这个想法只是放置四个隐形按钮,根据答案继续下一个问题或显示错误的答案图像。谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

您可以通过某种方式使您的视图透明化来解决它。但是,我建议另一个解决方案:添加一个UITapGestureRecognizer,每当它感应到一个水龙头时,检查它的坐标是否位于所需区域之一(对应于你想象的按钮)。

答案 1 :(得分:2)

为什么不使用轻拍手势识别器?

选项1

第1步:将tapGestureRecognizer添加到图片视图

for (UIImageView * v in @[v1, v2, v3, v4]) {
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
}

第2步:接收水龙头

- (void) handleTap:(UITapGestureRecognizer *)tap {
    if (tap.view == v1) {
        NSLog(@"V1 was tapped");
    }
    else if (tap.view == v2) {
        NSLog(@"V2 was tapped");
    }
    else if (tap.view == v3) {
        NSLog(@"V3 was tapped");
    }
    else if (tap.view == v4) {
        NSLog(@"V4 was tapped");
    }
}

选项2 - Michael Knudsen的回答

以下是迈克尔建议的替代方法

第1步:将tapGestureRecognizer添加到图片视图

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[imageViewsParentView addGestureRecognizer:tap];

第2步:接收水龙头

- (void) handleTap:(UITapGestureRecognizer *)tap {
    CGPoint positionInView = [tap locationInView:tap.view];

    if (CGRectContainsPoint(v1.frame, positionInView)) {
        NSLog(@"V1 was tapped");
    }
    else if (CGRectContainsPoint(v2.frame, positionInView)) {
        NSLog(@"V2 was tapped");
    }
    else if (CGRectContainsPoint(v3.frame, positionInView)) {
        NSLog(@"V3 was tapped");
    }
    else if (CGRectContainsPoint(v4.frame, positionInView)) {
        NSLog(@"V4 was tapped");
    }    
}

答案 2 :(得分:1)

您可以使用touchesBegan方法来检测点击的图像。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if([touch view] == answerImg1) //answerImg1 as your first image
    {
        // Do whatever you want when first image tapped
    }
    else if([touch view] == answerImg2)
    {
         // Do whatever you want when second image tapped
    }
    else if([touch view] == answerImg3)
    {
        // Do whatever you want when third image tapped
    }
    else
    {
        NSLog(@"Other view tapped");
    }


}