我有一个“热点视图”数组,它只是UIImage的,还有一个stackview数组,每个包含两个标签。我试图得到热点视图图像的颜色和其中一个标签更改为红色时,热点视图被点击。在一天的大部分时间里,我似乎都不知道怎么用谷歌搜索。任何洞察都是伟大的。
下面是我的代码:
我已经在tap手势函数中注释了我希望实现的功能,但是我不知道如何访问stackview中的嵌套标签,也不知道im是否正确使用了tap手势识别器。

    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()
        }
    }

重要的部分是在热点视图上传递.tag,然后通过
stackView[index].arrangedSubviews.first作为?UILabel公司
谢谢

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

10-14 17:18