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

时间:2016-08-31 13:48:45

标签: ios swift

我有一个“热点视图”数组,它们只是UIImage和一组stackviews,每个视图包含两个标签。我试图获取热点视图图像的颜色,并在点击热点视图时将其中一个标签更改为红色。我似乎无法通过谷歌搜索一天的大部分时间。任何见解都会很棒。

以下是我的代码:

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

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

感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:0)

想出来。请参阅以下代码:

    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中的标签 stackView [index] .arrangedSubviews.first as?的UILabel

感谢