【问题标题】:Multiple paths in CAShapeLayerCAShapeLayer 中的多条路径
【发布时间】:2012-04-15 13:35:09
【问题描述】:

我想知道是否有可能让 CAShapeLayer 笔触不止一条路径,因为实际上它只需要一条路径作为参数,例如:

CAShapeLayer* myLayer=[CAShapeLayer layer];
myLayer.path=myBezierPath

但是如果我想在该层上绘制多个路径怎么办?

【问题讨论】:

  • 好吧,我自己发现了。可以做到:好的,我自己发现了。可以执行以下操作: CGMutablePathRef combinedPath= CGPathCreateMutableCopy(path.CGPath); CGPathAddPath(combinedPath, NULL, path2.CGPath); CGPathAddPath(combinedPath, NULL, path3.CGPath); CGPathAddPath(combinedPath, NULL, path4.CGPath); myLayer.path=combinedPath;
  • 您最好在上面发表您的评论作为答案。 :)
  • iJatrat - 很好的答案,但您需要发布并接受它作为答案

标签: ios cocoa-touch cocoa core-animation core-graphics


【解决方案1】:

使用

void CGPathAddPath (
  CGMutablePathRef path1,     // The mutable path to change.
  const CGAffineTransform *m, // A pointer to an affine transformation matrix, or NULL if no transformation is needed. If specified, Quartz applies the transformation to path2 before it is added to path1.
  CGPathRef path2             // The path to add.
);

代码示例:

CGMutablePathRef combinedPath = CGPathCreateMutableCopy(path.CGPath);
CGPathAddPath(combinedPath, NULL, path2.CGPath);
CGPathAddPath(combinedPath, NULL, path3.CGPath);
CGPathAddPath(combinedPath, NULL, path4.CGPath);
myLayer.path = combinedPath;

【讨论】:

    【解决方案2】:

    Swift 4.1 版本的@Kjuly's answer

    在 Swift 中你可以使用CGMutablePath

    代码示例:

    let combinedPath = CGMutablePath()
    combinedPath.addPath(path1)
    combinedPath.addPath(path2)
    ....
    combinedPath.addPath(path_n)
    
    myLayer.path = combinedPath
    

    【讨论】:

      【解决方案3】:

      另一种方法是像这样附加路径......

      UIBezierPath *left_path = [UIBezierPath bezierPathWithRect:frame_left];
      UIBezierPath *right_path = [UIBezierPath bezierPathWithRect:frame_right];
      
      [left_path appendPath:right_path];
      
      CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      shapeLayer.path = left_path.CGPath;
      

      【讨论】:

        【解决方案4】:

        好的,我自己发现了。可以做到以下几点:

        CGMutablePathRef combinedPath= CGPathCreateMutableCopy(path.CGPath); 
        CGPathAddPath(combinedPath, NULL, path2.CGPath); 
        CGPathAddPath(combinedPath, NULL, path3.CGPath); 
        CGPathAddPath(combinedPath, NULL, path4.CGPath); 
        myLayer.path=combinedPath;
        

        【讨论】:

          【解决方案5】:
          import UIKit
          import QuartzCore
          import CoreGraphics
          
          class ViewController: UIViewController,UIGestureRecognizerDelegate {
          var btnview : UIButton!
          var buttonCenter = CGPoint.zero
          var firstlayerpoint = CGPoint.zero
          var firstLayer = CAShapeLayer()
          var secondLayer = CAShapeLayer()
          var thirdLayer = CAShapeLayer()
          var initialPosition = CGRect()
          let label = UILabel()
          @IBOutlet weak var nameLabel: UILabel!
          @IBOutlet weak var blueLabel: UILabel!
          @IBOutlet weak var greenLabel: UILabel!
          @IBOutlet weak var redLabel: UILabel!
              override func viewDidLoad() {
              super.viewDidLoad()
          
          
                  firstLayer = self.createCircleWithBounds(bounds: CGRect(x:0, y:0, width:100,height:100), Position: self.view.center, StrokeColor: UIColor.blue, LineWidth: 20.0)
                  firstLayer.strokeStart = 0.00
                  firstLayer.strokeEnd = 0.33
          
                  self.view.layer.addSublayer(firstLayer)
          
          
                  secondLayer = self.createCircleWithBounds(bounds: CGRect(x:0, y:0, width:100,height:100), Position: self.view.center, StrokeColor: UIColor.red, LineWidth: 20.0)
                  secondLayer.strokeStart = 0.33
                  secondLayer.strokeEnd = 0.66
                  self.view.layer.addSublayer(secondLayer)
          
          
                  thirdLayer = self.createCircleWithBounds(bounds:  CGRect(x:0, y:0, width:100,height:100), Position: self.view.center, StrokeColor: UIColor.green, LineWidth: 20.0)
                  thirdLayer.strokeStart = 0.66
                  thirdLayer.strokeEnd = 1.00
                  self.view.layer.addSublayer(thirdLayer)
          
              btnview = UIButton(frame: CGRect(x: self.view.center.x - 20 , y: self.view.center.y - 20 , width: 40, height: 40))
              btnview.backgroundColor = UIColor.gray
              btnview.isUserInteractionEnabled = true
              let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panButton(panGesture:)))
              // panGesture.minimumNumberOfTouches = 1
          
              btnview.addGestureRecognizer(panGesture)
          
              self.view.addSubview(btnview)
              // Do any additional setup after loading the view, typically from a nib.
                  nameLabel.isHidden = true
                  blueLabel.isHidden = true
                  greenLabel.isHidden = true
                  redLabel.isHidden = true
          }
          
          
          func panButton(panGesture: UIPanGestureRecognizer) {
          
              //let translation = panGesture.translation(in: self.btnview)
              panGesture.view!.center =  btnview.center
              panGesture.setTranslation(CGPoint.zero, in: self.view)
              // var point = CGPoint.zero
              //  point = firstLayer.frame.size.center
          
          
              if panGesture.state == .began {
          
          
                  label.isHidden = false
                  buttonCenter = btnview.center // store old button center
              }
              else if panGesture.state == .ended || panGesture.state == .failed || panGesture.state == .cancelled {
          
                  print(btnview.frame.origin.x)
                  print(greenLabel.frame.origin.x)
                  if btnview.frame.origin.x > greenLabel.frame.origin.x
                  {
                      //    lblflayer.isHidden = false
                      //    lblsecondlayer.isHidden = true
                      //    lblthirdlayer.isHidden = true
                      nameLabel.isHidden = false
                      nameLabel.text = "Blue"
                      nameLabel.backgroundColor = UIColor.blue
          
                  }
                  else if btnview.frame.origin.x > blueLabel.frame.origin.x
                  {
                      // print(btnview.frame.origin.x)
                      //  print(lblsecondlayer.frame.origin.x)
                      //     lblsecondlayer.isHidden = false
                      //   lblflayer.isHidden = true
                      //    lblthirdlayer.isHidden = true
                      nameLabel.isHidden = false
                      nameLabel.text = "Red"
                      nameLabel.backgroundColor = UIColor.red
          
                  }
          
                  else  if btnview.frame.origin.x > redLabel.frame.origin.x
                  {
                      print(btnview.frame.origin.x)
                      print(redLabel.frame.origin.x)
                      greenLabel.isHidden = true
                      //  lblsecondlayer.isHidden = true
                      //  lblthirdlayer.isHidden = false
          
                      nameLabel.isHidden = false
                      nameLabel.text = "Green"
                      nameLabel.backgroundColor = UIColor.green
                  }
                  else
                  {
                      nameLabel.isHidden = true
                      // lblflayer.isHidden = true
                      // lblsecondlayer.isHidden = true
                      //  lblthirdlayer.isHidden = true
                  }
          
                  btnview.center = buttonCenter // restore button center
              }
              else
              {
                  let location = panGesture.location(in: view) // get pan location
                  btnview.center = location // set button to where finger is
              }
          
          
          }
          
          
          
          func createCircleWithBounds(bounds: CGRect, Position position: CGPoint, StrokeColor color: UIColor, LineWidth lineWidth: CGFloat) -> CAShapeLayer {
              //let shapelayer = CAShapeLayer.layer
              let shapelayer = CAShapeLayer()
              shapelayer.strokeColor = color.cgColor
              shapelayer.fillColor = UIColor.clear.cgColor
              shapelayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.width / 2).cgPath
              shapelayer.bounds = bounds
              shapelayer.position = position
              shapelayer.lineCap = kCALineCapButt
              shapelayer.lineWidth = lineWidth
              return shapelayer
          }
          
          override func didReceiveMemoryWarning() {
              super.didReceiveMemoryWarning()
              // Dispose of any resources that can be recreated.
          }
          

          }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多