Find the touched Area (country)on a UIImageView based on coordinates - ios

I am Using A UIImageView with a globe image,if i touch on any region Example if i touch on any African country,I would like to fire a touch event and show that it is a particular country tapped by the user.I approached the way to get he co-ordinates on the images,Please give me any suggestions how can i get the touched positions country wise.
Thanks in advance.

Put the Image in UIImageView of a ViewController in Portrait mode
implement
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
println(touch.locationInView(self.view))
}
result
(155.5,164.5)

Related

Defining my spaceship movement in spritekit game in Swift

Hi I am new to Xcode and Swift, right now I am trying to design a game that involves a spaceship as player with alien spaceships.
I ran into a little problem where I am trying to differentiate the movement of the spaceship from the firing of the spaceship.
Basically I used touchesBegan() function to run the function which my spaceship fires, and touchesMoved() function to move the spaceship's x-position.
These are the code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
pShoot()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
spaceship.run(SKAction.moveTo(x: location.x, duration: 0.5))
}
}
What I am trying to do is to differentiate the clicking or touching indicator and pressed and move indicator, in other words I dont want the spaceship to fire when I am pressed and move on the screen, and I do not want the spaceship to move when I am clicking constantly but at different position. ( touchesMoved() detects changes in touch positions so if I am clicking at different positions spaceship will move which I dont want)
I would like to know what would be the best way of implementing this, thank you.
You could try checking the area that the user pressed and deciding whether or not to move the spaceship depending on the origin of the touch.

Is it possible to trigger an action while the user's finger slides into a particular area?

I am working on an iOS app and I want to have the effect of an action while the user's finger slides into a particular area. I first thought UIButton with touch dragged in will work, but it still requires a touch before the "drag". So is there a way to do it? Thanks so much!
You could use UITouch with touch began. Have your particular area view as a UIView IBOutlet. Lets call it particularAreaView
Maybe something like this:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let touch = touches.allObjects[0] as UITouch //get the latest touch
let touchLocation = touch.locationInView(self.view)
let particularAreaViewFrame = self.view.convertRect(particularAreaView.frame, fromView: self.view)
//Check if touch began is in your particularAreaViewFrame
if CGRectContainsPoint(particularAreaViewFrame, touchLocation) {
tiggerYourActionHere()
}
}
If you need it to consider touches that is moving into the area, you will have to override touchesMoved: withEvent: as well. Then maybe refactor the above into a function that use in both places.

Differentiating touches based on first or second

I'm trying to implement 2 distinct touch based behaviours. The first behaviour's logic is like this:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
moveViewTo(touchLocation) //moves my view to touchLocation
}
Now, for my second behaviour, I want to rotate the view based on where I have my finger on the screen. For this, I tried this:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
rotateViewTo(touchLocation) //rotates my view to face touchLocation
}
Now, I want these two distinct behaviours to work concurrently. In particular, I want the first touch to change the position of the view, and the second touch to rotate the view.
Rotating is possible only if there are two touches in the view, and changes direction based on the second touch.
Is there any way to differentiate which of the touches are first and which are second? I can't find a way to differentiate from the touches: Set<NSObject> because by nature Set is an unordered collection.
I believe your only option is to store your first touch as a var. My swift isn't strong enough to write out the code for you but here are the steps.
Loop through your touches instead of just grabbing the first one.
Check if there is a firstTouch (your var for tracking first touch)
If there isn't a firstTouch assign the first touch and do your position logic
If there is a first touch check to see if each touch is the firstTouch. If it isn't then do your rotation logic.
On touches ended and canceled loop through each touch. If it is the firstTouch set your var to nil.
Sorry I can't write all the code out for you, but hopefully that gets you going in the right direction.

Detect separate touches on different SpriteKit nodes?

I am making a SpriteKit game where in order to begin the game, you need to hold two separate spots (SKShapeNodes) for 3 seconds. If you let go either finger or move either finger off a node, the game will not start. I have it working fine with 1 spot, but when I try to do 2 spots, I'm stuck. What is the simplest way to detect the 2 correct touches on the correct nodes?
This doesn't seem like a very uncommon situation, so if anyone knows the best way to handle this, I would appreciate the help.
Swift preferred, also.
Set multipleTouchEnabled to YES and use the touchesForView: method.
You can get more specific information on multi touch from the Apple Docs Multitouch Events.
The main idea is to have all touches when users provides any actions and operate them at the same time.
So, add 3 handlers to your Scene class:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
checkTouches((event?.allTouches())!)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
checkTouches((event?.allTouches())!)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
checkTouches((event?.allTouches())!)
}
In the checkTouches function you will see all touches with updated properties (like positions etc).
Simple example:
func checkTouches(touches: Set<UITouch>) {
// iterate over all touches
for t in touches {
let touch = t as UITouch
let touchLocation = touch.locationInNode(self)
if... <-- YOUR CODE HERE TO CHECK NODE NAME AND TOUCHED TIME
}
}
Using this approach you will be able to handle any changes simultaniously.
E.g. user may touch on your node, then move finger outside it and then move bach to this node.
Enjoy!

Sprite kit touch and hold with swift

i'm try to make my first game in sprite kit using swift
everything work fine for now but i couldn't know how could handle touch and hold in the screen
i'm try to make a jump power when the player hold the touch but i can't find event for this
thanks ;)
you can try this :
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// action when the user touches the screen
// you can know where did he touch the screen like this
let touchLocation = touches?.anyObject().locationInView(self/* or your view */)
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
// your code here
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
// your code here
}
those method will help cary touches events in your screen
As I understand you need to handle 2 situations
1 - player taps on the screen/node - e.g. to jump
2 - player taps and holds - e.g. to change jump power
I think you need to handle both "touchesBegan" and "touchesEnded".
In "touchesBegan" start special timer and after some delay (e.g. 0.5 secs) start playing special animation for it (jump power indicator that shows current jump power)
on "touchesEnded" - stop timer, stop animation and calculate result jump power (based on timer's value).
If you also need to handle direction (angle) of jump - in this case you should also handle "touchesMoved" and calculate current angle based on touch position.

Resources