Error with "touchesBegan" - ios

My code:
var location = CGPoint(x:0,y:0)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as? UITouch
location = touch.locationInView(self.view)
Button.center = location
}
Gives an error on this line:
let touch = touches.anyObject() as? UITouch
How to fix it?

Try this : -
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches {
if let touch = touches.first as? UITouch {
if touch.tapCount == 2{
//Do Something here
}
}
}
}

try this
if let touch = touches.first as? UITouch
{
}

Related

How to detect precisely when a SKShapeNode is touched?

I'm working with Swift and SpriteKit.
I have the following situation :
Here, each of the "triangles" is a SKShapenode.
My problem is that I would like to detect when someone touches the screen which triangle is being touched.
I assume that the hitbox of all these triangles are rectangles so my function returns me all the hitboxes touched while I only want to know which one is actually touched.
Is there any way to have a hitbox that perfectly match the shape instead of a rectangle ?
Here's my current code :
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches.first
let touchPosition = touch!.locationInNode(self)
let touchedNodes = self.nodesAtPoint(touchPosition)
print(touchedNodes) //this should return only one "triangle" named node
for touchedNode in touchedNodes
{
if let name = touchedNode.name
{
if name == "triangle"
{
let triangle = touchedNode as! SKShapeNode
// stuff here
}
}
}
}
You could try to use CGPathContainsPoint with a SKShapeNode instead of nodesAtPoint, which is more appropriate:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches.first
let touchPosition = touch!.locationInNode(self)
self.enumerateChildNodesWithName("triangle") { node, _ in
// do something with node
if node is SKShapeNode {
if let p = (node as! SKShapeNode).path {
if CGPathContainsPoint(p, nil, touchPosition, false) {
print("you have touched triangle: \(node.name)")
let triangle = node as! SKShapeNode
// stuff here
}
}
}
}
}
This would be the easiest way of doing it.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
for touch in touches {
let location = touch.locationInNode(self)
if theSpriteNode.containsPoint(location) {
//Do Whatever
}
}
}
The way I do this with Swift 4:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let touchPosition = touch.location(in: self)
let touchedNodes = nodes(at: touchPosition)
for node in touchedNodes {
if let mynode = node as? SKShapeNode, node.name == "triangle" {
//stuff here
mynode.fillColor = .orange //...
}
}
}

How do I get a CGPoint from a touchesBegan event?

In my AppDelegate, I want to detect when a tap event has been made to the status bar. In order to do so, I need to get the CGPoint from the event. How do I get it from this code?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
let location = // how to get a CGPoint ????
let statusBarFrame = UIApplication.sharedApplication().statusBarFrame
if CGRectContainsPoint(statusBarFrame, location){
print("Status bar touched")
}else{
print("Not touched")
}
}
You're using Swift 2; you could retrieve the tap location as follows:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
if let touch = touches.first as? UITouch
{
//The view you would like to get the tap location from.
let tapPoint = touch.locationInView(self.view)
let statusBarFrame = UIApplication.sharedApplication().statusBarFrame
if CGRectContainsPoint(statusBarFrame, tapPoint)
{
print("Status bar touched")
}
else
{
print("Not touched")
}
}
}
A UITouch object has a method locationInView: that allows you to find the location of a touch in a particular view. From the docs:
Returns the current location of the receiver in the coordinate system of the given view.
The view object in whose coordinate system you want the touch located. A custom view that is handling the touch may specify self to get the touch location in its own coordinate system. Pass nil to get the touch location in the window’s coordinates.
So try:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
guard let touch = touches.first else {
return
}
let location = touch.locationInView(nil)
let statusBarFrame = UIApplication.sharedApplication().statusBarFrame
if CGRectContainsPoint(statusBarFrame, location) {
print("Status bar touched")
} else {
print("Not touched")
}
}
This works regardless of device orientation
Swift 5.1
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
guard let touch = touches.first else {
return
}
let location = touch.location(in: nil)
}
You'd have to have a view that you touched. If you've managed to get your app delegate to receive touch events, you could try getting the locationInView: using the app delegates window.

how to override touch functions properly in iOS 9, swift 2

Well, I says on the lines changeColorTo(touch: touches.anyObject() as? UITouch)
this error "value of type "Set" has no member "anyObject"
How can I fix the problem?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
changeColorTo(touch: touches.anyObject() as? UITouch)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
changeColorTo(touch: touches.anyObject() as? UITouch)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
changeColorTo(touch: touches.anyObject() as? UITouch)
}
func changeColorTo(touch touch: UITouch? ) {
if let contact = touch {
changeColorTo(point: contact.locationInView(self))
}
}
func changeColorTo(point point: CGPoint ) {
// Change the color model to the color displayed at the given view coordinate
if let color = colorModel {
let bounds = self.bounds
if bounds.contains(point) {
color.hue = Float((point.x-bounds.minX)/bounds.width*360)
color.saturation = Float((point.y-bounds.minY)/bounds.height*100)
}
}
}
Thanks in advance!
You should not pass UITouch set to CGPoint.
Try the below to solve the issues,
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject()! as UITouch
let location = touch.locationInView(self)
changeColorTo(location)
}
func changeColorTo(point: CGPoint ) {
}

Getting touch coordinates in Swift 1.2

I'm trying to get the coordinates of a touch on the screen, but every time it just gives me 0, 0. I could only find basic help with this function in the new version of Swift, and nothing on this problem. What am I doing wrong here?
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInView(self.view)
NSLog("location: %d, %d", location.x, location.y)
}
Thanks!
As per swift 1.2
override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) {
if let touch = touches.first as? UITouch {
let location = touch.locationInView(self.view)
println("Location: \(location)")
}
}
As per swift 1.1
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as? UITouch
let location = touch?.locationInView(self.view)
println("Location: \(location)")
}
Tested.

Touch Location and touchesBegan in Xcode 6 [Obj-C --> Swift]

I am having trouble creating a function "touchesBegan" and then creating a UIPoint and UITouch constant or variable that holds an x and y coordinate. I have the exact code I want in Objective-C but I do not know what it's equivalent is in Swift. Here is the Objective-C code which I want to basically translate into Swift code...
NOTE: This is a Single View Application, NOT a game... Thanks in advance.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if (point.x < 160) {
var = 10;
}
else{
var = 20;
}
}
As of Swift 1.2, use this
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
var touch = touches.first as! UITouch
var point = touch.locationInView(self)
if point.x < 160 {
var = 10;
}
else{
var = 20;
}
}
Where's the problem?
var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)
if point.x < 160 {
var variableName = 10;
}
else{
var variableName = 20;
}
Swift 1.2 changed the syntax for touchesBegan. See the UIResponder Reference.
func touchesBegan(_ touches: Set<NSObject>, withEvent event: UIEvent)
And don't forget to reference the super.
super.touchesBegan(touches, withEvent: event)
Here is an edited example of implementing this code from techotopia.com which might give you more information on the other three touches functions.
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)
// Insert if statements
super.touchesBegan(touches, withEvent: event)
}

Resources