Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在特定区域中移动对象_Ios_Iphone_Xcode_Swift_Drag And Drop - Fatal编程技术网

Ios 在特定区域中移动对象

Ios 在特定区域中移动对象,ios,iphone,xcode,swift,drag-and-drop,Ios,Iphone,Xcode,Swift,Drag And Drop,我在视图中有一个名为circle的圆和一条名为ground的线 我可以在整个视图中拖动和移动圆,但当我尝试限制它(在代码中)仅在地面区域下方移动时,它可以工作(当我尝试将其拖动到地面上方时),并且圆仅通过拖动x位置移动,但是当我试着把它拖回地面下时,这个圆只会随着x位置的拖动而移动,而y位置的拖动不会改变(y是地面的y) 我怎样才能使它在地面下以自由方式再次向后移动,而不仅仅是通过拖动的x位置? 以下是我迄今为止所做的尝试: let ground = SKSpriteNode() va

我在视图中有一个名为
circle
的圆和一条名为
ground
的线

我可以在整个视图中拖动和移动圆,但当我尝试限制它(在代码中)仅在地面区域下方移动时,它可以工作(当我尝试将其拖动到地面上方时),并且圆仅通过拖动x位置移动,但是当我试着把它拖回地面下时,这个圆只会随着x位置的拖动而移动,而y位置的拖动不会改变(y是地面的y)

我怎样才能使它在地面下以自由方式再次向后移动,而不仅仅是通过拖动的x位置? 以下是我迄今为止所做的尝试:

let ground = SKSpriteNode()
    var circle = SKShapeNode(circleOfRadius: 40)

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

//        ground.physicsBody?.dynamic = false
        ground.color = SKColor.redColor()
        ground.size = CGSizeMake(self.frame.size.width, 5)
        ground.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.size.height / 5)

        self.addChild(ground)

        circle.position = CGPointMake(CGRectGetMidX(self.frame) ,CGRectGetMinY(ground.frame) - (circle.frame.size.height / 2))
        circle.strokeColor = SKColor.blackColor()
        circle.glowWidth = 1.0
        circle.fillColor = SKColor.orangeColor()

        self.addChild(circle)

    }
    var lastTouch: CGPoint? = nil
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */
    let touch = touches.first
    let touchLocation = touch!.locationInNode(self)
        if circle.position.y < ground.position.y - (circle.frame.size.height / 2){
           circle.position = touchLocation
        } else {
            circle.position.x = CGFloat(touchLocation.x)
        }
    }
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let touchLocation = touch!.locationInNode(self)
        if circle.position.y < ground.position.y - (circle.frame.size.height / 2) {
            circle.position = touchLocation
        } else {
            circle.position.x = CGFloat(touchLocation.x)
        }
    }
let ground=SKSpriteNode()
变量圆=SKShapeNode(半径圆:40)
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
//地面。physicsBody?动态=假
ground.color=SKColor.redColor()
ground.size=CGSizeMake(self.frame.size.width,5)
ground.position=CGPoint(x:CGRectGetMidX(self.frame),y:self.frame.size.height/5)
self.addChild(地面)
circle.position=CGPointMake(CGRectGetMidX(self.frame)、CGRectGetMinY(ground.frame)-(circle.frame.size.height/2))
circle.strokeColor=SKColor.blackColor()
圆圈宽度=1.0
circle.fillColor=SKColor.orangeColor()
self.addChild(圆)
}
var lastTouch:CGPoint?=无
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
让我们先接触
让touchLocation=touch!.locationInNode(self)
如果圆位置y<地面位置y-(圆框架尺寸高度/2){
circle.position=触摸位置
}否则{
circle.position.x=CGFloat(touchLocation.x)
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让我们先接触
让touchLocation=touch!.locationInNode(self)
如果圆位置y<地面位置y-(圆框架尺寸高度/2){
circle.position=触摸位置
}否则{
circle.position.x=CGFloat(touchLocation.x)
}
}
我已经解决了这个问题:

touchsmoved
func中,我编写了以下代码:

for touch in touches {
            if touchLocation.y < ground.position.y {
                circle.position = touchLocation
            } else {
                circle.position.x = touchLocation.x
            }
        }
用于触控触摸{
如果触摸位置.y<接地位置.y{
circle.position=触摸位置
}否则{
圆圈.position.x=触摸位置.x
}
}