【问题标题】:Xcode how to add buttons to an image viewXcode如何将按钮添加到图像视图
【发布时间】:2018-02-05 06:24:24
【问题描述】:

我会采取什么方法在 xcode 中向图像添加按钮?该应用程序适用于 iPhone 和 iPad,因此它需要以某种方式“响应”。我正在尝试做类似这张图片的事情 has sample image map 我找到了一个使用 Obj-C 的 Github 项目,但我只是想学习应用程序开发并且不知道我在做什么。最终,一旦按下按钮,我将制作一些带有图像和一些信息的弹出窗口。我的应用程序很快。我将不胜感激和提示或指导教程。

【问题讨论】:

  • 你想在点击按钮或其他任何地方打开弹出窗口吗?
  • 是的。我想我知道如何做弹出部分。我只是不知道如何将按钮限制在图像的特定区域以适应多种屏幕尺寸。
  • 假设提供的地图是一张平面图。
  • @Druva 是的。它是一个png
  • 可以使用storyboard来添加。图像上的按钮,或者只是以编程方式添加和创建一个按钮,其大小限制为图像大小,我应该向您展示吗?还是你在问别的?

标签: swift xcode image dictionary button


【解决方案1】:

您可以在图像视图上使用 UITapGestureRecognizer 来执行与按钮操作相同的操作

第一次放入 viewDidLoad

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewControllerName.imgAction))

ImageViewName.addGestureRecognizer(tap)

第二次编写函数来执行操作

func imgAction(){

// perform the action here
}

【讨论】:

  • 如果你已经将图像视图放置在视图控制器中
【解决方案2】:

假设提供的地图是一张平面图像。 这是 sudo,它将向您解释这个概念。 适用于不同屏幕尺寸的 iPhone 或 iPad。

//make sure you have the correct size of original image.
let originalImageSize: CGSize = CGSize(width: 1000, height: 500)
//Lets create a struct to have the basic information we need
struct Item {
    var id: Int
    var name: String
    var x: Double
    var y: Double
}
//Creating some sample button on the map
//x and y are based on the original Image size
let buttonsInfo : [Item] = [
    Item(id: 1, name: "France", x: 10, y: 10),
    Item(id: 2, name: "Poland", x: 20, y: 30),
    Item(id: 3, name: "Location A", x: 50, y: 100),
    Item(id: 4, name: "Location B", x: 300, y: 300)
]

//sudo code : plug it with your project real image code
let yourUIImageView = UIImageView(image: UIImage())

//after the image is rendered on UI get the real dimensions
//Size of the Image that is rendered on UI in proper aspect ratio
let renderedImageSize: CGSize = CGSize(width: 500, height: 250)

for item in buttonsInfo {
    let button = UIButton()
    button.tag = item.id
    // 
    button.frame = CGRect(x: Double(renderedImageSize.width/originalImageSize.width)*item.x,
                      y: Double(renderedImageSize.height/originalImageSize.height)*item.y,
                      width: 50, height: 50)
    button.backgroundColor = UIColor.red
    button.setTitle("Name your Button ", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

    //yourUIImageView is actual view in your code
    yourUIImageView.addSubview(button)
}

func buttonAction(sender: UIButton!) {
    let item:Item = buttonsInfo.filter{$0.id == sender.tag}
    print("Button tapped")
    print(item)
}

注意:如果您在方向更改时调整 yourUIImageView 的大小,则需要再做一些更改以根据新的纵横比移动按钮。 (如果您需要帮助,请告诉我们)

【讨论】:

    【解决方案3】:

    有两种方法 1. 如图所示,可以给按钮添加图片

    1. 您可以在情节提要中放置一个图像,然后在其上方添加一个按钮,然后删除该按钮的文本。最好将这两个东西都添加到 UIView 中,这样您就可以确保按钮正好位于图像上方。添加图像和按钮后,您可以设置 UIView 的约束,并将顶部、左侧、右侧和底部的约束设置为 0

    注意:确保按钮始终位于图像上方,以便它可以点击。尝试为情节提要中的按钮着色以确定。

    【讨论】:

    • @Moaz Khan -- 这不是我要找的。我需要图像映射顶部的多个按钮。无论在哪个屏幕上查看地图,他们都需要停留在地图上的正确位置。
    • 如果您按照第 2 步操作,您可以这样做。你只需要弄清楚视图的约束。
    • 问题是,如果它在 iPhone 和 iPad 上,视图将会改变。有没有办法为按钮使用 x/y 坐标?然后我可以制作一个使用图像尺寸的公式吗??
    • 是的,如果您知道自动布局,您可以轻松做到这一点。这就是自动布局的原因。设置按钮相对于视图的坐标,只要您的约束是完美的,它将完美地设置为任何屏幕尺寸。
    • 我明白了。如何设置相对于视图的约束?在界面生成器中还是以编程方式?
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2020-08-24
    • 2011-09-13
    • 1970-01-01
    • 2015-04-18
    相关资源
    最近更新 更多