创建可点击的身体图

创建可点击的身体图

本文介绍了使用 Swift (iOS) 创建可点击的身体图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我已经在 HTML5 中制作的 iOS 应用程序 (Swift) 重新创建一些东西(使用 map 区域坐标).

I'm trying to recreate something for a iOS app (Swift) which I already made in HTML5 (using map area-coords).

我想展示一个与用户点击/触摸交互的人体图.身体存在比方说 20 个不同的部位,用户可以选择一个或多个身体部位.For every body-part there is a selected-state image that should appear when a part is selected.单击选定的部分将取消选择它.选择一个或多个部分后,用户可以继续,并将在下一个viewcontroller 中获得有关这些部分的一些信息.我附上一张简化的图片来解释我的目标.

I want to show a human body diagram that interacts with user clicks/touches. The body exists of let's say 20 different parts, and the user can select one or more body-parts. For every body-part there is a selected-state image that should appear when a part is selected. Clicking on a selected part will deselect it. After selecting one or more parts, the user can continue and will get some information about these parts in a next viewcontroller. I am attaching a simplified image to explain my goal.

有人能解释一下实现这一目标的最佳方法是什么吗?Swift 是否有类似的技术可以用来创建这样的交互式图像?

Can somebody explain what the best way is to achieve this goal? Is there a comparable technique that can be used in Swift to create such an interactive image?

谢谢!

推荐答案

已修复!

首先我像这样向 UIImageView 添加子层

First I added sublayers to the UIImageView like this

var path = UIBezierPath()
path.moveToPoint(CGPointMake(20, 30))
path.addLineToPoint(CGPointMake(40, 30))

// add as many coordinates you need...

path.closePath()

var layer = CAShapeLayer()
layer.path = path.CGPath
layer.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.5).CGColor
layer.hidden = true

bodyImage.layer.addSublayer(layer)

我覆盖了 touchesbegan 函数以便在点击形状时显示和隐藏.

Than I overrided the touchesbegan function in order to show and hide when the shapes are tapped.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first! as? UITouch {
        // get tapped position
        let position = touch.locationInView(self.bodyImage)

        // loop all sublayers
        for layer in self.bodyImage.layer.sublayers! as! [CAShapeLayer] {

            // check if tapped position is inside shape-path
            if CGPathContainsPoint(layer.path, nil, position, false) {
                if (layer.hidden) {
                    layer.hidden = false
                }
                else {
                    layer.hidden = true
                }
            }
        }
    }
}

这篇关于使用 Swift (iOS) 创建可点击的身体图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:56