2016-08-31 6 views
1

私は「ホットスポットビュー」の配列を持っています。これはUIImageだけで、それぞれに2つのラベルを含むstackviewsの配列です。ホットスポットビューの画像の色を取得しようとしていて、ホットスポットビューがタップされているときにラベルの1つが赤に変わることを試みています。私はほとんどの日のグーグルグーグルの後にどのように見つけることができないようです。どんな洞察も素晴らしいだろう。ここで 別の画像をタップすると、stackViewネストされたラベルの色をプログラム的に変更します。 Swift

は、以下の私のコードです:

私は私が達成するために望んでいたものをタップジェスチャー機能ではコメントアウトしているが、私は、タップジェスチャーを使用してイム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() 
     } 
    } 

ご協力いただきありがとうございます。

答えて

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 [インデックス] .arrangedSubviews.firstによってstackview内のラベルにアクセスすることでしたか? UILabel

ありがとう

関連する問題