Coordinates of image area tapped in swift programming - swift

I'm new to Swift app development and I would like to know how to add touch events based on where an Image is clicked using Swift. I need to get the coordinates of the area the image is tapped.

You'll need a tap gesture recognizer on the image view, you also need to set the user interaction property to enabled. Then you can get the point from the gesture recognizer's method. Here's some quick code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapAction:"))
self.imageView.userInteractionEnabled = true
self.imageView.addGestureRecognizer(tapGestureRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tapAction(sender: UITapGestureRecognizer) {
let touchPoint = sender.locationInView(self.imageView) // Change to whatever view you want the point for
}
}

UPDATE 2017:
Now Selector(String) is deprecated. One can use the new syntax #selector.
Also, the colon at the end is not needed.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))

Related

index tag UITapGestureRecognizer not responive when applied to outlet collection

When a UIView is touched it should move. When the UIView is touched nothing happens. All of the UIViews are connected in a outlet collection. No constraints are applied in the storyboard. I do not understand why nothing is happening when I touch the UIViews. My code is in swift.
import UIKit
class ViewController: UIViewController {
#IBOutlet private var cardView: [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
for index in cardView.indices {
let tap = UITapGestureRecognizer(target: self, action: #selector(tapCard(sender: )))
cardView[index].isUserInteractionEnabled = true
cardView[index].addGestureRecognizer(tap)
cardView[index].tag = index
}
}
#objc func tapCard (sender: UITapGestureRecognizer) {
let clickedView = cardView[sender.view!.tag]
print("View tapped !" , clickedView )
}
}
Make sure that your iboulet collection work correctly. Try change some property of that views...
And second, does you down through that code on touch and nothing happens, or you just doesn't rich that part of code on touch? thank you

Views not performing segue

I have a collection of views and I want to make that when they are tapped, it will perform the same segue. and no view performs any segue.
class ViewController: UIViewController {
#IBOutlet var categoryViews: [UIView]!
let tapGesture = UIGestureRecognizer(target: self, action: #selector(ViewController.move(tap:)))
override func viewDidLoad() {
super.viewDidLoad()
for category in (0..<categoryViews.count) {
categoryViews[category].addGestureRecognizer(tapGesture)
categoryViews[category].isUserInteractionEnabled = true
}
// Do any additional setup after loading the view.
}
#objc func move(tap: UIGestureRecognizer) {
performSegue(withIdentifier: "Animals", sender: nil)
}
}
A single instance of UITapGestureRecognizer can be added to a single view.
In your code, since you're using a single instance of UITapGestureRecognizer for each view, the tapGesture will be added only to the last view in categoryViews array.
You need to create different UITapGestureRecognizer instance for each view in categoryViews, i.e.
class ViewController: UIViewController {
#IBOutlet var categoryViews: [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
categoryViews.forEach {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(move(tap:)))
$0.addGestureRecognizer(tapGesture)
$0.isUserInteractionEnabled = true
}
}
#objc func move(tap: UITapGestureRecognizer) {
performSegue(withIdentifier: "Animals", sender: nil)
}
}
The problem is that this code doesn't do what you think it does:
class ViewController: UIViewController {
let tapGesture = UIGestureRecognizer(target: self, action: #selector(ViewController.move(tap:)))
Your let tapGesture is an instance property declaration, and what follows the equal sign is its initializer. But you can't speak of self in an instance property initializer; there is no instance yet. So self here is taken to be the class. Thus, your tap gesture recognizer "works", but the move message is not sent to your ViewController instance; in effect, it is sent into empty space.
To fix this, you can initialize tapGesture at a time when self does exist. For example:
class ViewController: UIViewController {
let tapGesture : UIGestureRecognizer!
func viewDidLoad() {
self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.move(tap:)))

How do I change the image of a UIbutton if selected?

I have a button called
#IBAction func likeIt(_ sender: Any) {
}
and I have two images called
like and likeSelected
how do I change the button picture when pressed and when not pressed
All you need to do is to give the button an initial image in the storyboard by setting the Image property.
Then after that you can set the button's image on highlighted state like below.
class ViewController: UIViewController {
#IBOutlet weak var buttonView: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
buttonView.setImage(UIImage(named: "likeSelected"), for: .highlighted)
}
}
Remember to check out Apple's documentation!
You're looking for UIButton.setImage(_:for:). For states, see UIControl.State.

UIPanGestureRecognizer does not work

It's weird, but when I add a UIPanGestureRecognizer to a view it does not work. It's my view, that white view is a subview of the red view:
Main View:
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.gestureRecognizers = [panRecognizer]
}
func handlePan(recognizer:UIPanGestureRecognizer) {
//foo
}
When I tap and drag the white view it won't move.
try this:
whiteView.isUserInteractionEnabled = true
From the Apple's docs:
If the code for your pan gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.
The number of touches is between the values specified in the minimumNumberOfTouches and maximumNumberOfTouches properties.
For a UIScreenEdgePanGestureRecognizer object, the edges property is configured and touches start at the appropriate edge.
Handling Pan Gestures
Here is the answer:
class ViewController: UIViewController {
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
var lastLocation:CGPoint = CGPointMake(0, 0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.addGestureRecognizer(panRecognizer)
}
func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(redView)
whiteView.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Promote the touched view
lastLocation = whiteView.center
}
}
If you are using swift language for method you can't use [].
var PanGesture = UIPanGestureRecognizer(target: self, action: "respondToPanGesture:")
whiteView.addGestureRecognizer(PanGesture)
Hope it helps.

Reveal Passcode dots using Swift

How do I reveal the passcode dots in swift, so then the user can see their password by clicking a button.
Thanks
Tyge
TextField has a property called secureTextEntry. All you need is to connect an outlet to your textfield and toggle secureTextEntry:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
passwordField.secureTextEntry = true
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showHide(sender: UIButton) {
passwordField.secureTextEntry = !passwordField.secureTextEntry
sender.setTitle({passwordField.secureTextEntry ? "Show":"Hide"}(), forState: .Normal)
}
}
stackoverflow provides a working search engine, but anyway here you is the answer.
Do the same, but with false instead of true
Obscure a UITextField password
By the way make sure that the user knows what he does when reveal the secure text.