gpt4 book ai didi

ios - 如何在平移手势事件期间转换一条线?

转载 作者:行者123 更新时间:2023-12-01 18:12:53 24 4
gpt4 key购买 nike

我正在创建一个流程图,当用户拖动一个节点时,我想移动连接到它的线。在节点的 pan 处理程序事件中,我实现了 CGAffineTransform转换线,但它不会平滑移动线。知道其他流程图应用程序如何处理这个问题吗?

func panHandler(sender: UIPanGestureRecognizer) {

else if (sender.state == .Changed)
{
let translation = sender.translationInView(self)
sender.view!.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y)


//rv is the connected line, created at an other place like:
//let w = GraphViewController.calcLength(e.x, y0: e.y, x1: e2.x, y1: e2.y)
//rv = UIView(frame: CGRectMake(0, 0, CGFloat(w), 1.0))
//var angle = GraphViewController.calcAngle(e.x, y0: e.y, x1: e2.x, y1: e2.y)
//rv.transform = CGAffineTransformMakeRotation(CGFloat(angle))

var angle = GraphViewController.calcAngle(e!.x, y0: e!.y, x1: r.destination!.x, y1:r.destination!.y)
rv!.transform = CGAffineTransformMakeRotation(CGFloat(angle))

}
}

最佳答案

因为您不需要在线上的任何触摸事件,我们可以使用简单的 CAShapeLayer并更新其在 pan 上的路径。我假设你想为这条线设置一些 anchor 。

为您的线定义一些 anchor

endOfLineAnchor = CGPoint(x: view.bounds.width / 2, y: view.bounds.height * 0.8)

创建一些 View 以将其附加到
ballView = createBallView()
ballView.center = CGPoint(x: view.bounds.width / 2, y: view.bounds.height / 2)

添加图层和 View
    lineLayer = CAShapeLayer()
lineLayer.strokeColor = UIColor.blackColor().CGColor
lineLayer.path = pathFromBallToAnchor()
view.layer.addSublayer(lineLayer)
view.addSubview(ballView)

在平移更新图层的路径
    else if (sender.state == .Changed)
{
let translation = sender.translationInView(self.view)
sender.view!.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y)
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
lineLayer.path = pathFromBallToAnchor()
CATransaction.commit()
}
CATransaction 的原因是我们要禁用隐式动画。

构建路径:
    func pathFromBallToAnchor() -> CGPathRef {
var bazier = UIBezierPath()
bazier.moveToPoint(ballView.center)
bazier.addLineToPoint(endOfLineAnchor)
return bazier.CGPath
}

结果:

Panned Ball attached to an anchored line in iPhone 5 iOS 8.1 simulator

关于ios - 如何在平移手势事件期间转换一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com