简体   繁体   English

如何触摸并按住屏幕左侧和右侧来移动图像? -迅捷

[英]How would I move my image with a touch and hold on the left and right side of the screen? - Swift

I need help with the movement in Swift. 我在Swift的运动中需要帮助。 I have a SKSpriteNode and I need help with touch and drag movement left or right to move and the SKSpriteNode . 我有一个SKSpriteNode ,我需要有关左右滑动和SKSpriteNode触摸和拖动运动的SKSpriteNode But only the left and right. 但是只有左右。 When I click on the other side than the x position of the picture is to just run over smoothly. 当我单击图片的x位置以外的另一侧时,它将平滑地越过。 Could anyone help how to do it? 有人可以帮忙吗? Thanks. 谢谢。

import SpriteKit

class PlayScene: SKScene {

    let rocket = SKSpriteNode(imageNamed: "rocket")

    override func didMoveToView(view: SKView) {
        rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
        self.addChild(rocket)
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        rocket.position = CGPoint(x: location.x, y: self.frame.height * 0.8)
    }
}

In this, the rocket animates for 1 second to the location of the touch and then moves with the touch if the user moves their finger. 在这种情况下,火箭会在触摸位置动画1秒钟,然后如果用户移动手指,则随触摸一起移动。

class GameScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
    rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
    self.addChild(rocket)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        rocket.position = location
    }
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let moveAction = SKAction.moveTo(location, duration: 1/*Or as long as you want it to move*/)
        rocket.runAction(moveAction)
    }
}
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速检测屏幕左侧和右侧的触摸 SKScene - Swift detecting touch in left and right side of screen SKScene 快速检测屏幕左右两侧的触摸 - Swift detect touch in both left and right side of screen simultaneously Swift 如何将触摸点击限制在手机的左侧和右侧 - Swift How can I restrict touch taps to the left and right side of phone 我如何继续在屏幕右侧的随机位置中生成SKSpriteNode,以从左侧的此随机位置转换它? - how would I keep spawning an SKSpriteNode in random locations on the right side of the screen to translate it from the this random location the left? 如何通过触摸按钮将UISlider缓慢左右移动? - How do I slowly move my UISlider to the left or right with a touch of a button? 如何在Swift中从左向右移动图像视图 - How to move image view from left to right in Swift 将UIBarButton移到右侧的UINavigationBar的左侧-Swift 2 - Move the UIBarButton on the right side for the left side of the UINavigationBar - Swift 2 如何检测iPhone屏幕右侧或左侧是否有水龙头? - How do I detect if there is a tap on right or left side of the iPhone screen? 像iPhone上的相册一样向左或向右移动图像 - Hold and move image left or right like photo gallery on iPhone 如何使用左侧或右侧移动UILabel? - How to move UILabel using left or right side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM