如何在UIView中引用BezierPath

时间:2017-10-30 20:07:11

标签: ios swift uibezierpath uitapgesturerecognizer

我试图在UIView中检测BezierPaths中的Taps,并且发现了许多对containsPoint方法的引用。但是,我似乎无法找到如何从我的ViewController实际引用BezierPaths。

我已经设置:

class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 69, height: 107), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct])
{

    let myPath = UIBezierPath()
    myPath.move(to: CGPoint(x: 32.24, y: 8.61))
    myPath.addLine(to: CGPoint(x: 31.99, y: 8.29))
    myPath.addLine(to: CGPoint(x: 31.78, y: 8.19))
    myPath.close()
}

Beziers是在这个函数中绘制的,名为:

override func draw(_ rect: CGRect)

在主ViewController中,我有以下功能来检测UIView上的Tap:

@objc func SATap(sender: UITapGestureRecognizer)
{
    let location = sender.location(in: self.SAView)

    // how do I call containsPoint from here?
}

如何从这里调用containsPoint?

bezierPaths在运行时正确绘制。

1 个答案:

答案 0 :(得分:0)

由于Paths是在单独的Class中的Class Function中创建的,因此我无法直接将它们保存到View中。

我通过将UIBezierPaths放入Dictionary然后返回Dictionary来解决这个问题。数组也可以工作,但这样我就可以轻松访问特定的路径。

Meteor.startup(() => {

  var postsmeta = JSON.parse(Assets.getText('postsmeta.json'));
  var posts = JSON.parse(Assets.getText('posts.json'));
  var length =  postsmeta.length;

  for(x=0; x < length; x++){

    Posts.insert({
      ID: posts[x].ID,
      post_content: posts[x].post_content
    });

    Postsmeta.insert({
      post_id: postsmeta[x].post_id,
      meta_value: postsmeta[x].meta_value
    });   

  }

});

然后我使用class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 71, height: 120), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct]) -> ([String: UIBezierPath]) { var Paths = [String: UIBezierPath]() let myPath = UIBezierPath() myPath.move(to: CGPoint(x: 32.24, y: 8.61)) myPath.addLine(to: CGPoint(x: 31.99, y: 8.29)) myPath.addLine(to: CGPoint(x: 31.78, y: 8.19)) myPath.close() Paths["mP"] = myPath let myPath2 = UIBezierPath() myPath2.move(to: CGPoint(x: 32.24, y: 8.61)) myPath2.addLine(to: CGPoint(x: 31.99, y: 8.29)) myPath2.addLine(to: CGPoint(x: 31.78, y: 8.19)) myPath2.close() Paths["mP2"] = myPath2 return Paths } 在视图中创建了图层,并在使用字典上的For In循环创建它们时为每个图层添加了View.addLayer属性:

Layer.name

然后我在手势函数中使用了词典:

for path in Paths.keys.sorted()
    {
        self.layer.addSublayer(CreateLayer(path)) // The Function simply creates a CAShapeLayer
    }

可能有更好的方法可以做到这一点,但这一切都有效并且表现良好。