gpt4 book ai didi

ios - 当点击单独的图像时,以编程方式更改 stackView 嵌套标签颜色。 swift

转载 作者:行者123 更新时间:2023-11-28 08:29:11 28 4
gpt4 key购买 nike

我有一组“热点 View ”,它们只是 UIImage 的,还有一组堆栈 View ,每个 View 包含两个标签。我正在尝试获取热点 View 图像的颜色以及点击热点 View 时更改为红色的标签之一。在谷歌搜索了一天的大部分时间后,我似乎无法找出方法。任何见解都会很棒。

下面是我的代码:

我已经在点击手势功能中注释掉了我希望实现的目标,但我不知道如何访问堆栈 View 中的嵌套标签,或者我是否正确使用了点击手势识别器。

    import UIKit
import OAStackView

protocol StandMapHotspotLayerViewDataSource {

func numberOfHotspots(standMapHotspotLayerView: StandMapHotspotLayerView) -> Int

func hotspotViewForIndex(index: Int, inStandMapHotspotLayerView: StandMapHotspotLayerView) -> (UIView, OAStackView)
}

struct HotspotDataSource {

var stackView: [OAStackView] = []
var hotspotView: [UIView] = []
}

class StandMapHotspotLayerView: UIView {

var dataSource: StandMapHotspotLayerViewDataSource?
var hotspotDataSource = HotspotDataSource()

override func layoutSubviews() {
super.layoutSubviews()

let hotspotCount = self.dataSource?.numberOfHotspots(self) ?? 0

(0..<hotspotCount).map({ index in
return self.dataSource!.hotspotViewForIndex(index, inStandMapHotspotLayerView: self)
}).forEach({ hotspotView, stackView in

hotspotDataSource.hotspotView.append(hotspotView)
hotspotDataSource.stackView.append(stackView)

hotspotView.userInteractionEnabled = true
let gesture = UITapGestureRecognizer(target: hotspotView, action: #selector(self.hotspotWasPressed(_:)))
self.addGestureRecognizer(gesture)

self.addSubview(hotspotView)
self.addSubview(stackView)
})

addLine()
}

func hotspotWasPressed(sender: UITapGestureRecognizer) {
//
// sender.numberOfTouchesRequired = 1
//
// let hotspotView = hotspotDataSource.hotspotView[index]
// let stackView = hotspotDataSource.stackView[index]
//
// hotspotView.tintColor = UIColor(red: 157, green: 27, blue: 50, alpha: 1)
// stackView
}

func addLine() {
let path = UIBezierPath()
let shapeLayer = CAShapeLayer()
for index in 0..<self.dataSource!.numberOfHotspots(self) {
let stackView = hotspotDataSource.stackView[index]
let hotspotView = hotspotDataSource.hotspotView[index]
if stackView.frame.origin.y < 100 {
let stackViewPoint = CGPointMake(stackView.frame.origin.x + stackView.frame.size.width / 2, stackView.frame.origin.y + stackView.frame.size.height)
let imageViewPoint = CGPointMake((hotspotView.frame.origin.x + hotspotView.frame.size.width / 2), hotspotView.frame.origin.y)
path.moveToPoint(stackViewPoint)
path.addLineToPoint(imageViewPoint)
} else {
let stackViewPoint = CGPointMake(stackView.frame.origin.x + stackView.frame.size.width / 2, stackView.frame.origin.y)
let imageViewPoint = CGPointMake((hotspotView.frame.origin.x + hotspotView.frame.size.width / 2), hotspotView.frame.origin.y + hotspotView.bounds.size.height)
path.moveToPoint(stackViewPoint)
path.addLineToPoint(imageViewPoint)
}
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = UIColor.whiteColor().CGColor
shapeLayer.lineWidth = 0.2
shapeLayer.fillColor = UIColor.whiteColor().CGColor
self.layer.addSublayer(shapeLayer)
}
}

func reloadData() {
self.setNeedsLayout()
}
}

提前感谢您的帮助。

最佳答案

想通了。见下面的代码:

    import UIKit
import OAStackView

protocol StandMapHotspotLayerViewDataSource {

func numberOfHotspots(standMapHotspotLayerView: StandMapHotspotLayerView) -> Int

func hotspotViewForIndex(index: Int, inStandMapHotspotLayerView: StandMapHotspotLayerView) -> (UIImageView, OAStackView)
}

struct HotspotViews {

var stackView: [OAStackView] = []
var hotspotView: [UIImageView] = []
}

class StandMapHotspotLayerView: UIView {

var dataSource: StandMapHotspotLayerViewDataSource?
var hotspotViews = HotspotViews()

override func layoutSubviews() {
super.layoutSubviews()

let hotspotCount = self.dataSource?.numberOfHotspots(self) ?? 0

var i: Int = 0

(0..<hotspotCount).map({ index in
return self.dataSource!.hotspotViewForIndex(index, inStandMapHotspotLayerView: self)
}).forEach({ hotspotView, stackView in

hotspotViews.hotspotView.append(hotspotView)
hotspotViews.stackView.append(stackView)

hotspotView.userInteractionEnabled = true
hotspotView.tag = i
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.hotspotWasPressed(_:)))
hotspotView.addGestureRecognizer(gesture)

self.addSubview(hotspotView)
self.addSubview(stackView)

i += 1
})

addLine()
}

func hotspotWasPressed(sender: UITapGestureRecognizer) {
let index = sender.view!.tag
let hotspot = hotspotViews.hotspotView[index]
let textLabel = hotspotViews.stackView[index].arrangedSubviews.first as? UILabel

hotspot.image = UIImage(named: "RedHotspotImage")
textLabel?.textColor = UIColor(red: 158/255, green: 27/255, blue: 50/255, alpha: 1)


//go to hotspot url: StandMapView.hotspots[index].url
}

func addLine() {
let path = UIBezierPath()
let shapeLayer = CAShapeLayer()
for index in 0..<self.dataSource!.numberOfHotspots(self) {
let stackView = hotspotViews.stackView[index]
let hotspotView = hotspotViews.hotspotView[index]
if stackView.frame.origin.y < 100 {
let stackViewPoint = CGPointMake(stackView.frame.origin.x + stackView.frame.size.width / 2, stackView.frame.origin.y + stackView.frame.size.height)
let imageViewPoint = CGPointMake((hotspotView.frame.origin.x + hotspotView.frame.size.width / 2), hotspotView.frame.origin.y)
path.moveToPoint(stackViewPoint)
path.addLineToPoint(imageViewPoint)
} else {
let stackViewPoint = CGPointMake(stackView.frame.origin.x + stackView.frame.size.width / 2, stackView.frame.origin.y)
let imageViewPoint = CGPointMake((hotspotView.frame.origin.x + hotspotView.frame.size.width / 2), hotspotView.frame.origin.y + hotspotView.bounds.size.height)
path.moveToPoint(stackViewPoint)
path.addLineToPoint(imageViewPoint)
}
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = UIColor.whiteColor().CGColor
shapeLayer.lineWidth = 0.2
shapeLayer.fillColor = UIColor.whiteColor().CGColor
self.layer.addSublayer(shapeLayer)
}
}

func reloadData() {
self.setNeedsLayout()
}
}

重要的部分是在热点 View 上传递 .tag,然后通过以下方式访问堆栈 View 中的标签stackView[index].arrangedSubviews.first 作为?界面标签

谢谢

关于ios - 当点击单独的图像时,以编程方式更改 stackView 嵌套标签颜色。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39251501/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com