I need to rotate an UIImageview on user's motion using just one finger motion. For that I use a rotation gesture and then override touchesMoved with this code:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
if touch!.view === rotateImageView {
let position = touch!.locationInView(self.view)
let target = rotateImageView.center
let angle = atan2(target.y-position.y, target.x-position.x)
rotateImageView.transform = CGAffineTransformMakeRotation(angle)
}
}
This works as long as the user stop to touch the screen and make the motion on another part of the screen. On that scenario the UIImageview rotates on undesired position when the user starts to move the finger. I need to keep the original rotation position of the UIImageview to avoid this but I have no idea how to do it.
This gif shows the current behavior on the project prototype I have. As you can see when I start to rotate on the right side the flame icon suddenly appears near the touch point, that's what I want to prevent.
UPDATE Aug 23-2016
I used Matt and Dasem suggestion of using CGAffineTransformRotate but I still see the jump. I took Dasem code and adapted to my project like follows (Let me know if the adaption is wrong):
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
origin = (touches.first?.locationInView(self.view))!
//save the current transform
tempTransform = rotateImageView.transform
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchPoint = touches.first?.locationInView(self.view)
xOffSet = CGVector(dx:(origin.x)-rotateImageView.center.x, dy:(origin.x) - rotateImageView.center.y)
yOffSet = CGVector(dx:touchPoint!.x - rotateImageView.center.x, dy:touchPoint!.y - rotateImageView.center.y)
let angle = atan2(yOffSet.dy,yOffSet.dx) - atan2(xOffSet.dy,xOffSet.dx)
rotateImageView.transform = CGAffineTransformRotate(tempTransform, angle)
}
This change produces a similar effect when user touches a different region of the touchscreen. In this gif I've changed the image and remove the icons to make the effect more noticeable.
UPDATE WITH SOLUTION
Reapplied code with Jasem update and it worked , the following code solved the problem without using subclassing
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
origin = (touches.first?.locationInView(self.view))!
xOffSet = CGVector(dx:(origin.x)-rotateImageView.center.x, dy:(origin.y) - rotateImageView.center.y)
startingAngle = atan2(xOffSet.dy,xOffSet.dx)
//save the current transform
tempTransform = rotateImageView.transform
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchPoint = touches.first?.locationInView(self.view)
yOffSet = CGVector(dx:touchPoint!.x - rotateImageView.center.x, dy:touchPoint!.y - rotateImageView.center.y)
let angle = atan2(yOffSet.dy,yOffSet.dx)
let deltaAngle = angle - startingAngle!
rotateImageView.transform = CGAffineTransformRotate(tempTransform, deltaAngle)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
startingAngle = nil
}
//reset in case drag is cancelled
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
rotateImageView.transform = tempTransform
startingAngle = nil
}
you may want to change self.superview to self.view (mine is subclass)
var xOffSet:CGVector = CGVectorMake(0, 0)
var yOffSet:CGVector = CGVectorMake(0, 0)
var origin:CGPoint = CGPointZero
var tempTransform=CGAffineTransform()
var startingAngle:CGFloat?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
origin = (touches.first?.locationInView(self.superview))!
xOffSet = CGVector(dx:(origin.x)-self.center.x, dy:(origin.y) - self.center.y)
startingAngle = atan2(xOffSet.dy,xOffSet.dx)
//save the current transform
tempTransform = self.transform
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchPoint = touches.first?.locationInView(self.superview)
yOffSet = CGVector(dx:touchPoint!.x - self.center.x, dy:touchPoint!.y - self.center.y)
let angle = atan2(yOffSet.dy,yOffSet.dx)
let deltaAngle = angle - startingAngle!
self.transform = CGAffineTransformRotate(tempTransform, deltaAngle)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
startingAngle = nil
}
//reset in case drag is cancelled
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
self.transform = tempTransform
startingAngle = nil
}
The problem is that you are saying
rotateImageView.transform = CGAffineTransformMakeRotation(angle)
So whatever transform the image view has already (from the previous rotation) is thrown away and completely replaced by a new transform starting at angle. That's the "jump" you are seeing.
What you want to do is apply a rotation transform to the existing transform of the image view. You can do that by calling CGAffineTransformRotate, instead of CGAffineTransformMakeRotation.
Related
All i'm trying to do is be able to drag and drop a sprite across the screen. I've tried the following code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)
if ball.containsPoint(location) {
ball.position = location
}
}
}
This code does work, however, when I drag the ball quite fast, I guess it detects that the "ball" no longer contains the point "location" and the ball stops, meaning I have pick the ball up again. I want the ball to be able to respond to my touches quickly, so that the ball wont stop moving. How would I do this?
This is the correct way to do it in Sprite Kit. Like I said in my comment, you need to assign the moving node to an activate state, in this case I use a variable called movableNode to act is my activate state. When you touch the ball, it becomes activated by assigning it to movableNode. Then as you drag your finger, movableNode will go with the drag, Then once you release, you enter a deactivate state by setting movableNode to nil. Note that this code will only work on single touch applications, if you want to handle multitouch, then you need to track which touch is doing the dragging.
var movableNode : SKNode?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.locationInNode(self)
if ball.containsPoint(location) {
movableNode = ball
movableNode!.position = location
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first where movableNode != nil {
movableNode!.position = touch.locationInNode(self)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first where movableNode != nil {
movableNode!.position = touch.locationInNode(self)
movableNode = nil
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
movableNode = nil
}
}
KnightOFDragon solution works just fine. I just added few lines if you don't want to move sprite centre position to position where your finger touched the screen and you would like to move sprite from its centre original position.
var movableNode : SKNode?
var ballStartX: CGFloat = 0.0
var ballStartY: CGFloat = 0.0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
if (map?.contains(location))! {
movableNode = ball
ballStartX = (movableNode?.position.x)! - location.x // Location X of your sprite when touch started
ballStartY = (movableNode?.position.y)! - location.y // Location Y of your sprite when touch started
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first, movableNode != nil {
let location = touch.location(in: self)
movableNode!.position = CGPoint(x: location.x+ballStartX, y: location.y+ballStartY) // Move node around with distance to your finger
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let _ = touches.first, movableNode != nil {
movableNode = nil
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
movableNode = nil
}
I have an implementation where I've subclassed a UIImageView and called it a "DraggableImage"
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
originalPosition = self.center
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self.superview)
self.center = CGPoint(x: position.x, y: position.y)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.center = originalPosition
}
I am migrating my game from swift 1 and I can't figure out how to fix this function. I am trying to detect if no touches are taking place, which should keep the ball still. this is the code i have and keep getting error Invalid redeclaration of touchesBegan. Im not sure how to work around this, I've been stuck for a while and can't get the ball to stop moving when no touches present. Please help, thanks
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// do nothing if ad is showing
if Ad.share().showing {
return
}
let touch = touches.first! as UITouch
let touchedLocation = touch.locationInNode(self)
// change the speedXFirst of the ball if it starts
if start {
// start move
let moveRight = touchedLocation.x > CGRectGetMidX(self.frame)
let speedX = moveRight ? ballSpeedX : -ballSpeedX
ball.speedXFirst = speedX
}
// start game if it stops
else {
startGame()
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { //error RIGHT HERE
if start {
// stop move
ball.speedXFirst = 0
}
If you ball must be stopped when the user detaches the finger from the screen, probably you want to use:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
or this if you need to know when the touches were interrupted:
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}
I am making a basketball game. My problem is when I shoot the ball, i can then touch the ball in mid-air again, which i do not want. Once the user shoots the ball I do not want touch to have an effect. Here is my code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?
{
touching = false
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if ball.frame.contains(location){
touchingPoint = location
touching = true
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
touchingPoint = location
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if touching{
let dt:CGFloat = 8.25/29.5
let distance = CGVector(dx: touchingPoint.x-ball.position.x, dy: touchingPoint.y-ball.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
ball.physicsBody!.velocity=velocity
}
}
What can i add to my program so that a touch won't have any effect once the ball has been shot?
after you shoot the ball disable userInteraction
ball.userInteractionEnabled = false
then enable it again when you want them to be able to touch it
What I want to do is making a SkspiteNode move from left to right and back. This happens on touch.
I have this code:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches{
touchLocation = touch.locationInNode(self)
mainGuy.position.x = touchLocation.x;
}
}
This works. But when I have/move my finger at the right side of the screen and the mainGuy is at the left side of the screen it moves to my finger. This is not what I want to happen.
When I move my finger over the screen it needs to 'follow' my finger from the current position of the mainGuy.
How can I do this?
Do something like:
var touchesBeganPosition: CGPoint?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touchesBeganPosition = touches.first?.locationInNode(self)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
touchLocation = touch.locationInNode(self)
if let touchesBeganPosition = touchesBeganPosition {
mainGuy.position.x = touchLocation.x - touchesBeganPosition.x
}
}
}
This is untested of of course, but should be enough to get the general idea.
Ive wrote a few simple lines in swift 2.0 using SpriteKit to experiment using CGPathCreateMutable() to draw and creating lines shown below:
var lineNode = SKShapeNode()
var pathToDraw = CGPathCreateMutable()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
CGPathMoveToPoint(pathToDraw, nil, location.x, location.y)
lineNode.path = pathToDraw
lineNode.strokeColor = SKColor.redColor()
self.addChild(lineNode)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let positionInScene = touch.locationInNode(self)
CGPathAddLineToPoint(pathToDraw, nil, positionInScene.x, positionInScene.y)
lineNode.path = pathToDraw
//pathToDraw
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
lineNode.removeFromParent()
}
A red path is drawn and then is removed after I lift my finger. I am able to remove the lineNode sprite. However I am not able to remove the path after drawing? When drawing the second line I am able to see the 'remnant' of the first path. If I keep drawing the app will crash. Ive found that it used to be removed by doing
CGPathRelease(pathToDraw) //in obj-C at least
but then Ive found out i no longer have to do this because it is an autonomous process?
How do I remove it? Thanks!
You never reset the path and in the touches began callback you just add the next point. so to get rid of the old line just add
pathToDraw = CGPathCreateMutable()
to the end of
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
This will give you a new path to work with for the next line you draw