使用Swift(iOS)创建可点击的身体图 [英] Create clickable body diagram with Swift (iOS)
问题描述
我正在尝试为 HTML5 中使用 HTML5 创建的iOS应用(Swift)重新创建内容(使用 map
area-coords
)。
I'm trying to recreate something for a iOS app (Swift) which I already made in HTML5 (using map
area-coords
).
我想显示一个与用户点击/触摸交互的人体图。身体存在让我们说20个不同的部分,用户可以选择一个或多个身体部位。对于每个身体部位,都有一个选定状态的图像,应该在选择一个部分时出现。单击选定的部件将取消选择它。选择一个或多个部件后,用户可以继续并在下一个 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)创建可点击的身体图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!