I'm trying to get the coordinates of a touch on the screen, To show a popup menu
how to use in page view controller
What am I doing wrong here?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let position = touch.locationInView(view)
print(position)
}
}
In PageViewController,if you want to get pop up
In viewDidLoad:
let tap = UITapGestureRecognizer(target: self, action: "showMoreActions:")
tap.numberOfTapsRequired = 1
view.addGestureRecognizer(tap)
Make the page view controller inherit UIGestureRecognizerDelegate then add:
func showMoreActions(touch: UITapGestureRecognizer) {
let touchPoint = touch.locationInView(self.view)
let DynamicView = UIView(frame: CGRectMake(touchPoint.x, touchPoint.y, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
self.view.addSubview(DynamicView)
}
UIPageViewController with TouchEvent
Swift 5:
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(touchedScreen(touch:)))
view.addGestureRecognizer(tap)
}
#objc func touchedScreen(touch: UITapGestureRecognizer) {
let touchPoint = touch.location(in: self.view)
print(touchPoint)
}
Related
I want to use double click. I have written function doubleTap. How to recognize location of finger?
override func viewDidLoad()
{
super.viewDidLoad()
let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTap")
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
view.addGestureRecognizer(doubleTap)
}
func doubleTap()
{
}
You can use location(ofTouch:in:) to get the location of the touch. However, you need access to the gesture recognizer from inside the function where you want to access the location, so you should declare doubleTap as an instance property of your class.
class YourViewController: UIViewController {
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(YourViewController.doubleTap))
override func viewDidLoad(){
super.viewDidLoad()
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
view.addGestureRecognizer(doubleTap)
}
func doubleTap(){
let touchLocation = doubleTap.location(ofTouch: numberOfTouches-1,in: nil)
}
}
Change the input parameters to the function if you want to change whether you need to get the first or last touch's location or if you want to get the location relative to a subview.
You can get the gesture recognizer as a parameter
override func viewDidLoad()
{
super.viewDidLoad()
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapFunc))
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
view.addGestureRecognizer(doubleTap)
}
func doubleTapFunc(_ sender: UITapGestureRecognizer)
{
// Use 'sender' here to get the location
if sender.state == .ended {
// handling code
let location = sender.location(ofTouch: 0, in: self.view!)
}
}
I am trying to determine the location of a touch on a PDFView. I have set my pdf view to be user interacted as follows:
pdfView?.isUserInteractionEnabled = true
I then use the following function to detect touches:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: pdfView)
print(position)
}
}
The touchesBegan code is never fired.
Add gesture recognizer to PDFView like below:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
tapGesture.numberOfTapsRequired = 1
if var recognizer = pdfView.gestureRecognizers {
recognizer.append(tapGesture)
pdfView.gestureRecognizers = recognizer
}
I'm trying to add a swipe gesture for multiple nodes at once but it seems to only be working for the first node. When I try and swipe the other nodes it doesn't register there is a swipe. There is only one node but it is copied many times in the .sks file to fill the screen. All the nodes have the same name since it's a copy, I'm hoping to reuse them to fill the screen back up as soon as one of the nodes is swiped off.
let plankName = "woodPlank"
class PlankScene: SKScene {
var plankWood : SKSpriteNode?
var plankArray : [SKSpriteNode] = []
override func didMove(to view: SKView) {
plankWood = childNode(withName: "woodPlank") as? SKSpriteNode
let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
}
func swipedRight(sender: UISwipeGestureRecognizer) {
if sender.direction == .right {
let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5)
let nodeFinishedMoving = SKAction.removeFromParent()
let waitForNode = SKAction.wait(forDuration: 0.5)
plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]),
completion:{
})
})
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
print("\(plankWood?.name)")
if plankWood?.name == plankName {
print("Plank touched")
}
}
override func enumerateChildNodes(withName name: String, using block: #escaping (SKNode, UnsafeMutablePointer<ObjCBool>) -> Void) {
let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))
swipeRight.direction = .right
view?.addGestureRecognizer(swipeRight)
}
}
I'm trying to get the (x,y) location of where a button is tapped. I've found answers but they don't work for Swift 2.3.
Any suggestions?
That's what I have tried:
#IBAction func buttonTapped(sender: AnyObject, forEvent event: UIEvent) {
let buttonView = sender as! UIView;
// get any touch on the buttonView
if let touch = event.touchesForView(buttonView) as? UITouch{
// print the touch location on the button
print(touch.locationInView(buttonView))
}
}
Swift 2:
#IBAction func buttonPressed(button: UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches()?.first else { return }
let point = touch.locationInView(button)
print ("point: \(point)")
}
Swift 3:
#IBAction func buttonPressed(_ button: UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: button)
print ("point: \(point)")
}
To create an IBAction with event pick the right option in this popup in your storyboard:
func addGesture() {
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.buttonTapped(sender:)));
gestureRecognizer.numberOfTapsRequired = 1;
gestureRecognizer.numberOfTouchesRequired = 1;
button.addGestureRecognizer(gestureRecognizer) }
func buttonTapped(sender:UIGestureRecognizer) {
print(sender.location(in: button))
}
You can add Gesture recognizer to UIButton and it will work as well
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let touch = touches.allObjects[0] as UITouch
let touchLocation = touch.locationInView(self.view)
}
Hope this will be useful
I want to drag textField around my view. but im having small problem with my code.
here my code
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch : UITouch! = touches.first as! UITouch
location = touch.locationInView(self.view)
bottomTextField.center = location
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch : UITouch! = touches.first as! UITouch
location = touch.locationInView(self.view)
bottomTextField.center = location
}
Problem is if i tried to touch on the TextField and drag then it doesn't work. but if i just touch somewhere else and drag my finger then TextField start to drag. i want t make it only if i touch on that textFiel.
You can achieve this by adding a gesture to the text field:
override func viewDidLoad() {
super.viewDidLoad()
var gesture = UIPanGestureRecognizer(target: self, action: Selector("userDragged:"))
bottomTextField.addGestureRecognizer(gesture)
bottomTextField.userInteractionEnabled = true
}
func userDragged(gesture: UIPanGestureRecognizer){
var loc = gesture.locationInView(self.view)
self.bottomTextField.center = loc
}
If it helps anyone, following is an updated working version of the accepted answer for swift 4.0 and 5.0
override func viewDidLoad() {
super.viewDidLoad()
//Draggable textfields
let gesture = UIPanGestureRecognizer(target: self, action: #selector(userDragged(gesture:)))
bottomTextView.addGestureRecognizer(gesture)
bottomTextView.isUserInteractionEnabled = true
}
#objc func userDragged(gesture: UIPanGestureRecognizer){
let loc = gesture.location(in: self.view)
self.bottomTextView.center = loc
}