gpt4 book ai didi

ios - 用户触摸后线路旋转

转载 作者:行者123 更新时间:2023-11-30 14:10:20 27 4
gpt4 key购买 nike

我很难弄清楚应该如何创建一条从 A 点出发并指向 B 点(用户的手指)或更远的线。这样的行可能看起来像

(A) 原点 (B) 手指

A--------B------------

这条线会延伸到屏幕之外,并且当用户移动手指时应该旋转。创建直线并实时移动其 B 坐标的代码也会有所帮助。谢谢!

编辑 - 到目前为止我所做的:

class GameScene: SKScene {
let testball = SKShapeNode(circleOfRadius: 50)
let testguy = SKShapeNode(circleOfRadius: 50)
let line = SKShapeNode()
let pathToDraw = CGPathCreateMutable()


override func didMoveToView(view: SKView) {
self.backgroundColor = UIColor(colorLiteralRed: 0.9, green: 0.9, blue: 1, alpha: 0.5)
testball.fillColor = UIColor.redColor()
self.addChild(testball)

self.testguy.position = CGPointMake(CGRectGetMinX(self.frame) + testguy.frame.width + 10, CGRectGetMinY(self.frame) + testguy.frame.height + 10)
self.testguy.fillColor = SKColor.redColor()
self.addChild(self.testguy)

CGPathMoveToPoint(pathToDraw, nil, testguy.position.x, testguy.position.y)
self.addChild(self.line)

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

testball.position = (touches.first?.locationInNode(self))!

CGPathAddLineToPoint(pathToDraw, nil, (touches.first?.locationInNode(self).x)!, (touches.first?.locationInNode(self).y)!)
self.line.path = pathToDraw
self.line.strokeColor = UIColor.redColor()

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
testball.position = (touches.first?.locationInNode(self))!
//print(touches.first?.locationInView(self.view), appendNewline: true);

line.removeFromParent()

testball.position = (touches.first?.locationInNode(self))!

CGPathAddLineToPoint(pathToDraw, nil, (touches.first?.locationInNode(self).x)!, (touches.first?.locationInNode(self).y)!)
self.line.path = pathToDraw
self.line.strokeColor = UIColor.redColor()
self.addChild(line)
}

最佳答案

我明白了!我所要做的就是将 touchesBegantouchesMoved 中的代码更改为

    line.removeFromParent()

testball.position = (touches.first?.locationInNode(self))!

let pathToDraw = CGPathCreateMutable()
CGPathMoveToPoint(pathToDraw, nil, testguy.position.x, testguy.position.y)
CGPathAddLineToPoint(pathToDraw, nil, (touches.first?.locationInNode(self).x)!, (touches.first?.locationInNode(self).y)!)

self.line.path = pathToDraw
self.line.strokeColor = UIColor.redColor()

self.addChild(self.line)

touchesEnded内部,我编写了self.line.removeFromParent(),以便在用户触摸屏幕后删除该行。

关于ios - 用户触摸后线路旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845370/

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