I am using SpriteKit animation in my iOS application at first time. My aim is to drag and drop label, image and textfield. I have done it for label and image using SKLabelNode and SKSpriteNode.
Globally I declared,
var deltaPoint = CGPointZero
var selectedNode = SKNode()
First I created label, image, textfield in didMoveToView method. Then added label and image as child node and textfield as subview like below mentioned code.
let textField = UITextField(frame: CGRectMake(self.size.width / 2, self.size.height / 2 + 20, 200, 40))
textField.center = view.center
textField.borderStyle = .RoundedRect
textField.tag = 101
textField.textColor = UIColor.blackColor()
textField.font = UIFont.systemFontOfSize(17.0)
textField.placeholder = "Enter your name here"
textField.backgroundColor = UIColor.whiteColor()
textField.autocorrectionType = .Yes
textField.keyboardType = .Default
textField.clearButtonMode = .WhileEditing
textField.enabled = true
view.addSubview(textField)
let imageNode = SKSpriteNode(imageNamed: "images.jpg")
imageNode.name = kMovableImageNodeName
imageNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(imageNode)
let labelNode = SKLabelNode(fontNamed: "Helvetica")
labelNode.name = kMovableLabelNodeName
labelNode.text = "Welcome"
labelNode.fontSize = 30
labelNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(labelNode)
In TouchesBegan method, set touched node to selected node.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
labelNode.zPosition = 0
imageNode.zPosition = 0
let positionInScene = touches.first?.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene!)
selectedNode = touchedNode
selectedNode.zPosition = 1
}
In TouchesMoved method, setting delta point value
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let currentPoint = touches.first?.locationInNode(self)
let previousPoint :CGPoint = (touches.first?.previousLocationInNode(self))!
deltaPoint = CGPointMake(currentPoint!.x - previousPoint.x, currentPoint!.y - previousPoint.y)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
selectedNode.zPosition = 1
deltaPoint = CGPointZero
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
selectedNode.zPosition = 0
deltaPoint = CGPointZero
}
In update method, setting new point to selected node
override func update(currentTime: CFTimeInterval) {
let newPoint = CGPointMake(selectedNode.position.x + self.deltaPoint.x, self.selectedNode.position.y + self.deltaPoint.y)
selectedNode.position = newPoint
deltaPoint = CGPointZero
}
Like this how to drag and drop the textfield and when user double tapped the textfield, have to show keyboard.
Related
override func didMove(to view: SKView) {
view.scene?.anchorPoint = CGPoint(x: 0,y : 0)
castle = SKShapeNode(circleOfRadius: ballRadius1)
castle.physicsBody = SKPhysicsBody(circleOfRadius:ballRadius1)
castle.fillColor = .white
castle.name = "castle"
castle.position = CGPoint(x: 0, y: 0)
castle.physicsBody?.isDynamic = false
castle.physicsBody?.affectedByGravity = false;
castle.isUserInteractionEnabled = true
self.addChild(castle)
I have a circle in the middle of the screen, I want to make it disappear when I tap it. Can you please help me? It is an SKShapeNode and has a PhysicsBody with the same radius
You could use touchesBegan and make it look like so
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
let nameOfTappedNode = nodeITapped.name
if nameOfTappedNode == "castle"{
//make it do whatever you want
castle.removeFromParent()
}
}
}
you could also implement it in touchesEnded instead if you want the node to disappear as soon as the user releases the touch.
I have a UIImage and I linked it into my code and named it "Person"
At the moment where ever I tap on the screen, the UIImage jumps to that position, but I want it so the image can ONLY move if I drag it. Here's my code:
var location = CGPointMake(0, 0)
#IBOutlet var Person: UIImageView!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch = touches.first as UITouch!
location = touch.locationInView(self.view)
Person.center = location
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch = touches.first as UITouch!
location = touch.locationInView(self.view)
Person.center = location
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Person.center = CGPointMake(160, 330)
}
I'd use gesture recognizers - much less to code. Also, I use a "three step" gesture in case there is more than one element to move.
Step #1: The user taps on a subview and a dashed border indicates that the subview is in edit mode.
Step #2: The user pans the subview to a new location in the superview.
Step #3: The user taps anywhere else in the superview (including a new subview to edit it next) to take the first subview out of edit mode.
Subview code:
var _editMode = false
var editMode:Bool {
return _editMode
}
var borderPath = UIBezierPath()
var dashedBorder = CAShapeLayer()
func drawDashedBorder() {
borderPath = UIBezierPath()
borderPath.move(to: CGPoint(x: 3, y: 3))
borderPath.addLine(to: CGPoint(x: self.frame.width-3, y: 3))
borderPath.addLine(to: CGPoint(x: self.frame.width-3, y: self.frame.height-3))
borderPath.addLine(to: CGPoint(x: 3, y: self.frame.height-3))
borderPath.close()
dashedBorder = CAShapeLayer()
dashedBorder.strokeColor = UIColor.white.cgColor
dashedBorder.fillColor = UIColor.clear.cgColor
dashedBorder.lineDashPattern = [2, 2]
dashedBorder.lineDashPhase = 0.0
dashedBorder.lineJoin = kCALineJoinMiter
dashedBorder.lineWidth = 1.0
dashedBorder.miterLimit = 10.0
dashedBorder.path = borderPath.cgPath
let animation = CABasicAnimation(keyPath: "lineDashPhase")
animation.fromValue = 0.0
animation.toValue = 15.0
animation.duration = 0.75
animation.repeatCount = 10000
dashedBorder.add(animation, forKey: "linePhase")
self.addSublayer(dashedBorder)
_editMode = true
}
func removeDashedBorder() {
dashedBorder.removeFromSuperlayer()
_editMode = false
}
This is not production code, so I never did quite figure out how to make the "moving dashed" animation run forever (which may be good, as it could be a performance issue).But 10000 seconds seems to be pretty generous for editing.
Here's the superview code, with the assumption you have imageView1 and imageView2 as subviews:
var editMode = false
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer()
let panGesture = UIPanGestureRecognizer()
tapGesture.numberOfTapsRequired = 1
tapGesture.addTarget(self, action: #selector(editImage))
self.addGestureRecognizer(tapGesture)
panGesture.addTarget(self, action: #selector(moveImage))
self.addGestureRecognizer(panGesture)
}
func editEye(_ recognizer:UITapGestureRecognizer) {
let p = recognizer.location(in: self)
if !editMode {
if (imageView1.layer.hitTest(p) != nil) {
imageView1.drawDashedBorder()
self.editMode = true
} else if (imageView2.layer.hitTest(p) != nil) {
imageView2.drawDashedBorder()
self.editMode = true
}
} else {
if imageView1.editMode {
imageView1.removeDashedBorder()
self.editMode = false
} else if imageView2.editMode {
imageView2.removeDashedBorder()
self.editMode = false
}
}
}
func moveEye(_ recognizer:UIPanGestureRecognizer) {
let p = recognizer.location(in: self)
if imageView1.editMode {
imageView1.position = p
}
if imageView2.editMode {
imageView2.position = p
}
}
This may not fit your exact needs, but the combination of tap & pan over tap for editing seems more natural. I'm sure that at the very least, doing a "silent" tap & pan (where it appears to the user they are simply dragging something around but they still have to tap on it first) is a better fit.
You can do this with touchesMoved event. But my recommendation is to use UIPanGestureRecognizer.
Declare a UIPanGestureRecognizer object and add it to your image view.
User interaction enable for image view.
Follow this nice apple documented sample code https://developer.apple.com/library/content/samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435
in the touchesBegan method: check if the touch hit the UIImageView and set a global boolean variable to true. Set it to false on touchesEnded, and check in touchesMoved if the variable is true before you move the UIImageview
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch = touches.first as UITouch!
location = touch.locationInView(self.view)
if(person.frame.origin.x<location.x&&person.frame.origin.x+person.frame.size.width>location.x{
// x value for touch is inside uiimageview
if(person.frame.origin.y<location.y&&person.frame.origin.y+person.frame.size.height>location.y{
//x&y value inside
personHit=true
}
}
// Person.center = location
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if(personHit){
let touch : UITouch = touches.first as UITouch!
location = touch.locationInView(self.view)
Person.center = location
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
personHit=false
}
Simple Approach to fix your problem:
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageView.isUserInteractionEnabled = true
let panGestureRecognizer = UIPanGestureRecognizer(target:self, action: #selector(panHandler(recognizer:)))
imageView.addGestureRecognizer(panGestureRecognizer)
}
func panHandler(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: view)
recognizer.view!.center = CGPoint(x: recognizer.view!.center.x + translation.x, y: recognizer.view!.center.y + translation.y)
recognizer.setTranslation(CGPoint.zero, in: view)
}
I was wondering if there was a way to give the properties of a UIButton like darkening the button once it has been pressed,... to a SKSpiteNode since an SKSpiteNode has more customization and because I am using SpriteKit. I have seen 1 other question like this but none of the answers worked. Here is the code I have to create the SKSpriteNode and to detect a touch on it:
import SpriteKit
class StartScene: SKScene {
var startButton = SKSpriteNode()
override func didMoveToView(view: SKView) {
startButton = SKSpriteNode(imageNamed: "playButton")
startButton.size = CGSize(width: 100, height: 100)
startButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 50)
self.addChild(startButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
if startButton.containsPoint(location){
// When it has been selected
}
}
}
//...
Pleas help. Thanks in advance... Anton
I have always achieved this by adding code to the touches began, and touches ended method. Inside of these methods I simply set the sprites color to black, and then change its color blend factor. Let me know if this works for you!
//This will hold the object that gets darkened
var target = SKSpriteNode()
//This will keep track if an object is darkened
var have = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var first = touches.first as! UITouch
var location:CGPoint = first.locationInNode(self)
touchP = location
mouse.position = touchP
var node:SKNode = self.nodeAtPoint(location)
if let button = node as? SKSpriteNode
{
target = button
have = true
}
else
{
have = false
}
if (have == true)
{
target.color = UIColor.blackColor()
target.colorBlendFactor = 0.2
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (havet == true)
{
target.color = UIColor.blackColor()
target.colorBlendFactor = 0
target = SKSpriteNode()
have = false
}
}
I want to increase a CGFloat every time while the Screen is tapped.
The float has a set maximum and minimum value.
I tried to go through this suggestion: StackOverflow
However, this only increases the CGFloat after the touch is made, or the maximum is reached. I want to increase the CGFloat during the touch, meaning the longer you touch the higher the Jump/CGFloat.
The problem probably lies within the impulse, that you cant change it after it was applied. That means, after the 'Player' gets an impulse of 20, and the screen is longer touched, the Float may increase, but the impulse won't.
If you look at my current code, the impulse is set at maximum while the screen is touched, but if released the action should be removed. However, it doesn't work, the impulse does not stop.
I know that you can set the velocity of the body at a value after the press is made, and if the press has ended the velocity back to 0 so it stops it 'jump', but that doesn't look quite smooth as it would be with an impulse.
Has anybody a solution?
struct Constants {
static let minimumJumpForce:CGFloat = 20.0
static let maximumJumpForce:CGFloat = 60.0
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var force: CGFloat = 20.0
func longPressed(longPress: UIGestureRecognizer) {
if (longPress.state == UIGestureRecognizerState.Began) {
println("Began")
self.pressed = true
let HigherJump = SKAction.runBlock({Player.physicsBody?.applyImpulse(CGVectorMake(0, Constants.maximumJumpForce))})
self.runAction(HigherJump , withKey:"HighJump")
}else if (longPress.state == UIGestureRecognizerState.Ended) {
println("Ended")
self.pressed = false
self.removeActionForKey("HighJump")
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
1.Create ‘Game’ from Xcode template based on SpriteKit
2.Copy paste listed code to GameScene class
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var location = CGPoint()
var floorSize = CGSize()
var floorColor = UIColor()
var player = SKSpriteNode()
override func didMoveToView(view: SKView) {
view.showsFPS = true;
view.showsNodeCount = true;
view.showsDrawCount = true;
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = 1
self.physicsBody?.contactTestBitMask = 1
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsWorld.contactDelegate = self;
location = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
player = SKSpriteNode(imageNamed:"Spaceship")
player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 320, height: 320))
player.physicsBody?.categoryBitMask = 1
player.physicsBody?.collisionBitMask = 1
player.physicsBody?.contactTestBitMask = 1
player.physicsBody?.linearDamping = 0;
player.xScale = 1
player.yScale = 1
player.position = location
self.addChild(player)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.physicsWorld.gravity = CGVectorMake(0, 0)
let direction = Float(1.5708)//Float(player.zRotation) + Float(M_PI_2)
player.physicsBody?.applyForce(CGVector(dx: 150000*CGFloat(cosf(direction)), dy: 150000*CGFloat(sinf(direction))))
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.physicsWorld.gravity = CGVectorMake(0, -7.9)
}
}
3.Run the app
This should give you start point for you 'Jump' game :)
Try changing this:
if(self.pressed){
let HigherJump = SKAction.runBlock({if(self.force < Constants.maximumJumpForce){
self.force += 2.0
}else{
self.force = Constants.maximumJumpForce
}})
self.runAction(HigherJump)
}
to this:
if(self.pressed){
if(self.force < Constants.maximumJumpForce) {
self.force += 2.0
}
else {
self.force = Constants.maximumJumpForce
}
}
Theres no need to use a runBlock SKAction here.
I'm new to swift programming and I decided I would make a simple game to start with SpriteKit. I have a SpriteNode that is supposed to pick 1 of 6 locations and move there when it is tapped, however from the methods I've seen I can't figure out how to implement it (again I'm new at this) Here is my code from the GameScene.swift file:
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let greenTileWidth = screenSize.width * 0.5
let greenTileHeight = screenSize.height * 0.33
let greenTilePositionY = [greenTileHeight / 2, greenTileHeight / 2 + greenTileHeight, greenTileHeight / 2 + greenTileHeight * 2 ]
let greenTilePositionX = [greenTileWidth / 2, greenTileWidth / 2 + greenTileWidth]
let backgroundTile = SKSpriteNode(imageNamed: "whiteTile")
backgroundTile.size.width = screenSize.width * 100
backgroundTile.size.height = screenSize.height * 100
addChild(backgroundTile)
let greenTile = SKSpriteNode(imageNamed: "greenTile")
greenTile.size.width = greenTileWidth
greenTile.size.height = greenTileHeight
greenTile.position.y = greenTilePositionY[0]
greenTile.position.x = greenTilePositionX[0]
greenTile.userInteractionEnabled = true
addChild(greenTile)
var randomX:Int = 0
var randomY:Int = 0
func getRandomY() -> Int{
randomY = Int(arc4random_uniform(26))%3
return randomY
}
func getRandomX() -> Int{
randomX = Int(arc4random_uniform(26))%2
return randomX
}
func moveGreenTile(){
greenTile.position.x = greenTilePositionX[randomX]
greenTile.position.y = greenTilePositionY[randomY]
}
getRandomX()
getRandomY()
moveGreenTile()
}
when the SpriteNode greenTile is tapped, getRandomY() getRandomX() and moveGreenTile() should be called.
First you have to set the name attribute of your SKSpriteNodes:
greenTile.name = "greenTile"
First I see some errors in your code. The return values of getRandomX and getRandomY never get really used. Because you set the randomX and randomY variables without actually calling getRandom. So you should update it to:
func moveGreenTile(){
greenTile.position.x = greenTilePositionX[getRandomX()]
greenTile.position.y = greenTilePositionY[getRandomY()]
}
That way you only have to call moveGreenTile and it will call the getRandom methods by itself.
Then you have to use the touchesBegan method to check if the user touches the screen. So with the name you can check if the user touched the greenTile by checking the name you've set earlier:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches{
let location = touch.locationInNode(self)
let node:SKNode = self.nodeAtPoint(location)
if(node.name == "greenTile"){
moveGreenTile()
}
}
}
This code detects tap events, not only touches, on a SKSpriteNode.
You can change how sensitive the tap gesture is by modifying TapMaxDelta.
class TapNode : SKSpriteNode {
// Tap Vars
var firstPoint : CGPoint?
var TapMaxDelta = CGFloat(10)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init() {
let texture = SKTexture(imageNamed: "Test.png")
super.init(texture: texture, color: UIColor.clear, size: texture.size())
isUserInteractionEnabled = true
}
// ================================================================================================
// Touch Functions
// ================================================================================================
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first {
firstPoint = firstTouch.location(in: self)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first, let firstPoint = firstPoint {
let curPoint = firstTouch.location(in: self)
if abs(curPoint.x - firstPoint.x) <= TapMaxDelta && abs(curPoint.y - firstPoint.y) <= TapMaxDelta {
print("tap yo")
}
}
}
}