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 ) {
}
Related
I'm implementing a passThroughView by creating a transparent View on top and override hitTest().
passThroughView should consume touches from Apple Pencil and if touch type is not from pencil, it pass touches to the view underneath.
The problems are:
parameter "event" in hitTest contains no touch, so I can't check touch type in hitTest
I can get touch from touchesBegan and check touch type, but it get called only after hitTest returned true
I subclass UIWindow and override sendEvent() but this function also called after hitTest (and I don't know why)
class WindowAbleToKnowTouchTypes: UIWindow {
override func sendEvent(_ event: UIEvent) {
if event.type == .touches {
// This get called after Hittest
if event.allTouches!.first!.type == .pencil {
print("This touch is from Apple Pencil")
}
}
super.sendEvent(event)
}
}
Is there anyway to check touchType to decide to pass or consume touches?
I ended up using a different approach, it could be useful for many cases: If I can't get touchType in hitTest(), I can still get the touchType with GestureRecognize:
class CustomGestureRecognizer : ImmediatePanGesture {
var beganTouch : UITouch!
var movedTouch : UITouch!
var endedTouch : UITouch!
override func shouldReceive(_ event: UIEvent) -> Bool {
// You can check for touchType here and decide if this gesture should receice the touch or not
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
// Save touch for later use
if let firstTouch = touches.first {
beganTouch = firstTouch
}
super.touchesBegan(touches, with: event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
// Save touch for later use
if let touch = touches.first {
movedTouch = touch
}
super.touchesMoved(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
// Save touch for later use
if let touch = touches.first {
endedTouch = touch
}
super.touchesEnded(touches, with: event)
}
}
In the target function of the gestureRecognizer, you can get the UITouch by:
let beganTouch = customGesture.beganTouch
let touchType = beganTouch.touchType
I am making a game similar to Fruit Ninja using Swift 2.0 in Xcode v7.0. However, in the touchesBegan, touchesMoved, and touchesCancelled override functions, I am getting the three following errors:
"Value of type 'Set' has no member 'anyObject'" comes up twice and
"Value of optional type 'Set' not unwrapped; did you mean to use "!" or "?"?" comes once.
Here is the code
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
activeSlicePoints.append(location)
redrawActiveSlice()
activeSliceBG.removeAllActions()
activeSliceFG.removeAllActions()
activeSliceBG.alpha = 1
activeSliceFG.alpha = 1
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
activeSlicePoints.append(location)
redrawActiveSlice()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
activeSliceBG.runAction(SKAction.fadeOutWithDuration(0.25))
activeSliceFG.runAction(SKAction.fadeOutWithDuration(0.25))
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
touchesEnded(touches, withEvent: event)
}
Here is an image of on what lines the errors appear
All help appreciated. Thanks in advance.
let touch = touches.first as? UITouch
This will get you the first touch object.
Use it like:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, withEvent:event)
}
I'm trying to get location of touch with swift2.1 and get error "Value of type 'UITouch' has no member 'locationInNode'".
here is my code
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let location = touch.locationInNode(self)
print(location)
}
}
I had a look into source and saw that in class UITouch there is only public
func locationInView(view: UIView?) -> CGPoint {
}
is it something I should use or ... ?
As NicolasMiari pointed out in the comment, only if you are using SpriteKit this will work. locationInNode() is a SpriteKit function that helps you find the point in an SKNode.
If you are using "standard" UIKit where you want to get the location of a touch in a view, you should be using locationInView() which takes a view(!) not the controller itself.
class ViewController : UIViewController{
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let location = touch.locationInView(self.view)
print(location)
}
}
}
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.
I am overriding:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
in Swift.
I am wondering, is this correct for getting one touch point:
var touchPoint: CGPoint! = event.touchesForView(self)?.anyObject()?.locationInView(self)
Thanks in advance!
As of iOS 8.3, touchesBegan takes a touches parameter which is a Set<NSObject>. It's now Set<UITouch>. So, you would:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: view) else { return }
// use `point` here
}
In previous iOS versions, touchesBegan was defined as a NSSet parameter, as outlined in the question, so you would:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if let touch = touches.anyObject() as? UITouch {
let touchPoint = touch.locationInView(view)
...
}
}