UILongPressGestureRecognizer does not do anything - ios

I wanted to have a UI button respond only if it is being held for more than a set number of seconds. So I used UILongPressGestureRecognizer as so:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var holdButton: UIButton!
#IBAction func holdButtonPressed(_ sender: Any) {
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressHappened))
recognizer.minimumPressDuration = 2.0
view.addGestureRecognizer(recognizer)
}
and the handler
#objc func longPressHappened(gestureReconizer: UILongPressGestureRecognizer){
holdButton.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
DispatchQueue.main.async {
print ("Sucess")
}
}
As you can see, i have used DispatchQueue and tried to change the color of the button but neither are working. Can someone please tell me why?
Update :-
I am confused with implementing the methods given in the answer so i thought i will give my full code again
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var holdButton: UIButton! {
didSet {
let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(longPressHappened))
recognizer.minimumPressDuration = 2.0
holdButton.addGestureRecognizer(recognizer)
}
}
override func viewDidLoad() {
super.viewDidLoad()
holdButton.layer.cornerRadius = 150
holdButton.layer.borderWidth = 1.0
holdButton.layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
holdButton.clipsToBounds = true
}
#objc func longPressHappened(gestureReconizer: UILongPressGestureRecognizer){
holdButton.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
DispatchQueue.main.async {
print ("Sucess")
}
}
}

You just need to create custom button using UIView. Add a long press gesture to that view and upon required time triggered the delegate/Closures.
func addLongPressGesture() {
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
recognizer.minimumPressDuration = 3.0 // Duration
customButton.addGestureRecognizer(recognizer)
}
#objc
func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
// Perform your functionality here
}
}

You need to add gesture to button instead of view
#IBOutlet weak var holdButton: UIButton! {
didSet {
let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(longPressHappened))
recognizer.minimumPressDuration = 2.0
holdButton.addGestureRecognizer(recognizer)
}
}

Add the gesture in your viewDidLoad instead:
override func viewDidLoad() {
super.viewDidLoad()
//...
let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(longPressHappened))
recognizer.minimumPressDuration = 2.0
holdButton.addGestureRecognizer(recognizer)
}

Whole isue is because, you are using UIBUtton for gesture recogniztion. you have to use Uiview for UILongPressGestureRecognizer .
If you want the animations like UIButton then you have to use manual animations or you can have ready to made code fro internet.
If you need further help, you can ask in comment

So I was looking at other answers and I found that all of them were in Objective-C which was why i posted this question... So because the answers were not working for me i used Swiftify to convert the code from This Question, and after some modification, it worked.
Here is the code snippet
override func viewDidLoad() {
super.viewDidLoad()
//...
let longPress_gr = UILongPressGestureRecognizer(target: self, action: #selector(doAction(_:)))
longPress_gr.minimumPressDuration = 2 // triggers the action after 2 seconds of press
holdButton.addGestureRecognizer(longPress_gr)
}
and then the objective-c function to make sure the code only gets triggered once after the long press happened
#objc func doAction(_ recognizer: UILongPressGestureRecognizer?) {
if recognizer?.state == .began {
print("sucess")
}
}
(However, I cant understand the difference between this answer and some of the answers given above... can someone comment on how it work)

Related

Follow and unfollowing button issue while closing & reopening the app

In the Tab Bar Controller here are four tabs namely home, discover, notification and user profile. The discover tab controller lists all the users in Firebase. The users are listed with username and follow button. If the current user taps on follow, the title is set to following.
protocol PeopleTableViewCellDelegate {
func goToProfileUserVC(userId: String)
}
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var followButton: UIButton!
var delegate: PeopleTableViewCellDelegate?
var user: User? {
didSet {
updateView()
}
}
func updateView() {
nameLabel.text = user?.username
if let photoUrlString = user?.profileImageUrl {
let photoUrl = URL(string: photoUrlString)
profileImage.sd_setImage(with: photoUrl, placeholderImage: UIImage(named: "placeholderImg"))
}
if user!.isFollowing! {
configureUnFollowButton()
} else {
configureFollowButton()
}
}
func configureFollowButton() {
followButton.layer.borderWidth = 1
followButton.layer.borderColor = UIColor(red: 226/255, green: 228/255, blue: 232.255, alpha: 1).cgColor
followButton.layer.cornerRadius = 5
followButton.clipsToBounds = true
followButton.setTitleColor(UIColor.white, for: UIControlState.normal)
followButton.backgroundColor = UIColor(red: 69/255, green: 142/255, blue: 255/255, alpha: 1)
followButton.setTitle("Follow", for: UIControlState.normal)
followButton.addTarget(self, action: #selector(self.followAction), for: UIControlEvents.touchUpInside)
}
func configureUnFollowButton() {
followButton.layer.borderWidth = 1
followButton.layer.borderColor = UIColor(red: 226/255, green: 228/255, blue: 232.255, alpha: 1).cgColor
followButton.layer.cornerRadius = 5
followButton.clipsToBounds = true
followButton.setTitleColor(UIColor.black, for: UIControlState.normal)
followButton.backgroundColor = UIColor.clear
followButton.setTitle("Following", for: UIControlState.normal)
followButton.addTarget(self, action: #selector(self.unFollowAction), for: UIControlEvents.touchUpInside)
}
func followAction() {
if user!.isFollowing! == false {
Api.Follow.followAction(withUser: user!.id!)
configureUnFollowButton()
user!.isFollowing! = true
}
}
func unFollowAction() {
if user!.isFollowing! == true {
Api.Follow.unFollowAction(withUser: user!.id!)
configureFollowButton()
user!.isFollowing! = false
}
}
override func awakeFromNib() {
super.awakeFromNib()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.nameLabel_TouchUpInside))
nameLabel.addGestureRecognizer(tapGesture)
nameLabel.isUserInteractionEnabled = true
}
func nameLabel_TouchUpInside() {
if let id = user?.id {
delegate?.goToProfileUserVC(userId: id)
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Here code for duplicate user:
func loadUsers() {
// self.users = []
API.User.observeUser { (user) in
self.isFollowing(userId: user.id!, completed: { (value) in
user.isFollowing = value
self.users.append(user)
self.tableView.reloadData()
})
}
}
My issue: The list show at Discover tab is duplicated and when user taps on the follow button, it's title is set to following. But if the user closes and reopens the app, upon return to Discover tab the previously followed user's button remains follow despite the data having been inserted correctly in Firebase.
Any help much appreciated please...
I am writting this as an answer as there is (probably) not enough space to write it as a comment.
First of all your set up looks a bit messy, it seems that some details are handled locally and some on Firebase, and nothing in your code is triggering loadUsers().
I would advice you to set up as following:
Create a class named followingUsers give them the necessary properties e.g following and set your initializer. Then add var followingUsersArray = [followingUsers]() inside your ViewController.
Put the obeserve Firebase function in your viewDidLoad(), get
all the data and put it in your followingUsersArray then reload your tableView.
Create a UITableViewCell class and give it the desired properties, then set a method to configure the cell. Call that method from cellAtRow and pass it the values from the followingUsersArray.

UITapGestureRecognizer does not work with UIView animate

I'm trying to call an action when I tap on a UIImage at the time of its animation.
I've seen similar questions, but I could not apply these solutions to my case.
Please help.
xcode 9.2
swift 4
import UIKit
class MyCalssViewController: UIViewController {
#IBOutlet weak var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// action by tap
let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.actionUITapGestureRecognizer))
myImageView.addGestureRecognizer(gestureSwift2AndHigher)
}
// action by tap
#objc func actionUITapGestureRecognizer (){
print("actionUITapGestureRecognizer - works!") // !!! THIS DOES NOT WORK !!!
}
// hide UIImage before appear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
myImageView.center.y += view.bounds.height
}
// show UIImage after appear with animation
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 10, animations: {
self.myImageView.center.y -= self.view.bounds.height
})
}
}
You just missed a line, enabling user interaction on the view.
override func viewDidLoad() {
super.viewDidLoad()
// action by tap
let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.actionUITapGestureRecognizer))
myImageView.isUserInteractionEnabled = true
myImageView.addGestureRecognizer(gestureSwift2AndHigher)
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.clickedImageView(_:)))
myImageview.isUserInteractionEnabled = true
myImageview.addGestureRecognizer(tapGesture)
// MARK: - Gesture Recognizer action
func clickedImageView(_ sender: UITapGestureRecognizer) {
// Write your action here.
}
You need to pass in the option .allowUserInteraction like this:
UIView.animate(withDuration: 10, delay: 0, options: .allowUserInteraction, animations: {
...
})

UIGestureRecognizer on only one part of the screen

If you look into the image, you'll see a textview, button and a slider.
I have completed all the steps except for the last one which is, when I tap on the slider, it constantly disappear. I know it disappear because I implemented UITapGestureRecognizer.
I guess my question is, I want the slider to disappear everytime I tap anywhere on the screen but when I am using the slider, I dont want the slider to disappear which is happening now every time I release my tap.
I have tried implementing one more UITapGestureRecognizer in sizeRefont with a function to keep sizeRefont.isHidden false but when I do that, the slider will not disappear whenever I tap on the screen.
I tried putting sizeRefont.isHidden = false in sizeRefont action and it doesnt work either.
class ResizeController: UIViewController {
#IBOutlet weak var sizeRefont: UISlider!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissRefontSize(_:)))
view.addGestureRecognizer(tap)
}
#IBAction func sizeRefont(_ sender: AnyObject) {
let fontSize = CGFloat(sizeRefont.value)
textView.font = UIFont(name: textView.font!.fontName, size: fontSize * 30.0)
}
#IBAction func showSlider(_ sender: Any) {
sizeRefont.isHidden = false
}
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
if sender.location(in: sizeRefont){
sizeRefont.isHidden = false
} else {
sizeRefont.isHidden = true
}
}
}
There is an error on if sender.location(in: sizeRefont) where it says CGPoint is not convertible to Bool
Image
First thing you need to do is you need to Adjust the dismissRefontSize() method to to the following :
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: view)
If sizeReFont.frame.contains(location) {
// do nothing
}else {
sizeReFont.isHidden = true
}
}
The other thing you need to adjust is creating the tap recognized in your viewDidLoad() to the following:
let tap = UITapGestureRecognizer(target: self, action : #selector(dismissRefontSize(_:)))

iOS Swift, cannot get pinch gesture to work

i have a test project that takes text from a file, adds it to a textview and displays it.
i want to add some gestures but cannot seem to make it work...
here is the relevant code:
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
#IBOutlet var textview1: UITextView!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(ViewController2.pinchRecognized(_:)))
self.view.addGestureRecognizer(self.pinchGesture)
}
#IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
self.textview1.addGestureRecognizer(pinchGesture)
self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
any ideas? followed several tutorials but none seem to help. code is tested on actual iPhone...
thanks a lot
Edit for Solution:
#IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
var pinchScale = pinchGesture.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize - pinchScale)
pinchScale = pinchGesture.scale
} else {
self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize + pinchScale)
pinchScale = pinchGesture.scale
}
}
thanks to nishith Singh
Try adding the gesture recogniser to your textview in viewDidLoad instead of adding it in pinchRecognized. Currently you are adding the pinchGesture to your view which is behind your text view and hence will not receive the touch
var pinchGesture = UIPinchGestureRecognizer()
Use this code:
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(_:)))
self.textview1.addGestureRecognizer(self.pinchGesture)
// Do any additional setup after loading the view.
}
#IBAction func pinchRecognized(_ pinch: UIPinchGestureRecognizer) {
let fontSize = self.textview1.font!.pointSize*(pinch.scale)/2
if fontSize > 12 && fontSize < 32{
textview1.font = UIFont(name: self.textview1.font!.fontName, size:fontSize)
}
}
You might have to hit and trial with the minimum and maximum font sizes as you want, right now the minimum font size is 12 and the maximum font size is 32.
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.pinchGesture))
func pinchGesture(sender: UIPinchGestureRecognizer){
sender.view?.transform = (sender.view?.transform)!.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
print("pinch gesture")
}
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
#IBOutlet var textview1: UITextView!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchRecognized:")
self.view.addGestureRecognizer(self.pinchGesture)
}
func pinchRecognized(pinch: UIPinchGestureRecognizer) {
self.textview1.addGestureRecognizer(pinchGesture)
self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
Your code is actually working. But not the way you want probably.
Initially you assigned the gesture recogniser to the view of view controller.
But then inside the method you added same gesture recogniser to the UITextView.
So it should be working on UITextView. And the gesture recogniser is removed from the view controller's view. Gesture recogniser can only have one target. Pick view controller's view or textview.
You set the delegate of self.pinchGesture before initialising.
Initialise the self.pinchGesture first.
Set the delegate.
Add self.pinchGesture to self.view
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(ViewController2.pinchRecognized(_:)))
self.view.addGestureRecognizer(self.pinchGesture)

GestureRecognizer not responding to tap

After initialisation of by subclass of UIImageView I have the following line of code:
self.userInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
I created the necessary associated function :
func handleTap(gestureRecognizer: UITapGestureRecognizer) {
print("In handler")
}
On tapping on the view in question, "In handler was never printed to the console". I then removed the handler function to see if the compiler would complain about the missing function. It didn't.
I'm positively stumped. I'd truly appreciate any light people can shed on this.
Update: My class is actually a UIImageView as opposed to UIView
I was using UITapGestureRecognizer that I placed on a UILabel using Storyboard.
To get this to work I also had to place a checkmark in the block labeled: "User Interaction Enabled" in the UILabel Attributes Inspector in the Storyboard.
I discovered the answer after carefully combing through my code.
One of the parent views was created without supplying a frame:
While it's a noobish enough error to warrant deletion of this questions, odds are someone else will also have the same issue in the future...
Try this
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.view.userInteractionEnabled = true
var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
self.view.addGestureRecognizer(tapGesture)
}
func handleTap(sender : UIView) {
println("Tap Gesture recognized")
}
In addition to the other answers, this can be caused by adding the gesture recognizer to multiple views. Gesture recognizers are for single views only.
Reference: https://stackoverflow.com/a/5567684/6543020
I ran into this problem with programmatic views.
My UIView with the gesture recognizer had .isUserInteractionEnabled = true, but it did not respond to taps until I set .isUserInteractionEnabled = true for its parent views as well.
Most likely you add UIGestureRecognizer in wrong place. Here is working sample with UIView from storyboard. If you create your UIView dynamically then you should put this initialization in the correct constructor.
class TestView: UIView
{
override func awakeFromNib()
{
self.userInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}
func handleTap(gestureRecognizer: UITapGestureRecognizer)
{
println("Here")
}
}
I found the solution to this problem after lot of trial and error. So there are two solution two this
1. Either add the GestureRecognizer in viewDidLoad() and turn
userInteractionEnabled = true
2. If using computed property use lazy var instead of let to the property.
lazy var profileImageView: UIImageView = {
let iv = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
iv.image = #imageLiteral(resourceName: "gameofthrones_splash")
iv.contentMode = .scaleAspectFill
iv.translatesAutoresizingMaskIntoConstraints = false
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
iv.isUserInteractionEnabled = true
return iv
}()
For anyone that is still having problems even with all the previous answers, make sure you are using UITapGestureRecognizer instead of UIGestureRecognizer, I kept missing this detail while trying to find what was wrong.
This Code works for me with XCode 7.0.1
import UIKit
class ImageView: UIImageView {
init(frame: CGRect, sender: Bool, myImage: UIImage) {
super.init(frame: frame)
self.image = myImage
initBorderStyle(sender)
// enable user interaction on image.
self.userInteractionEnabled = true
let gesture = UITapGestureRecognizer(target: self, action: "previewImage:")
addGestureRecognizer(gesture)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func previewImage(myGesture: UITapGestureRecognizer? = nil) {
print("i'm clicked")
}
private func initBorderStyle(sender: Bool) {
self.layer.masksToBounds = true
self.layer.cornerRadius = 8
self.layer.borderWidth = 0.5
self.layer.borderColor = getBorderColor(sender)
self.backgroundColor = getColor(sender)
}
func getBorderColor(sender: Bool) -> CGColor {
var result: CGColor
if sender {
result = UIColor(red: 0.374, green: 0.78125, blue: 0.0234375, alpha: 0.5).CGColor
} else {
result = UIColor(red: 0.3125, green: 0.6015625, blue: 0.828125, alpha: 0.5).CGColor
}
return result
}
}
Xcode 11.4 Swift 5.2
UserInteraction is enabled by default on Custom UIView
import UIKit
class YourView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.addGestureRecognizer(tapGesture)
}
#objc func tapped() {
// do something
}
}
If you want to be notified elsewhere that the custom UIView has been tapped, then it is convenient to use a Notification.
It is best to create this as an extension to prevent it being Stringly typed.
extension Notification.Name {
static let DidTapMyView = Notification.Name("DidTapMyView")
}
In your custom UIView when the tapped function is called
#objc func tapped() {
NotificationCenter.default.post(name: .DidTapMyView, object: self)
}
In your UIViewController where you want to listen for the Notification:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(myViewWasTapped), name: .DidTapMyView, object: nil)
}
#objc func myViewWasTapped() {
// Notified here - do something
}
Here's a list of my findings in 2021, XCode 13:
Make sure both your view and all of its superviews have set width & height constraints(this one is crucial)
Set isUserInteractionEnabled = true for your view
There's no need to set explicit frame or isUserInteractionEnabled for other super views
In addition the above (userInteractionEnabled, clipsToBounds, etc.), if you are working with a view in a child view controller be sure that you have added it as a child with myParentViewController.addChild(myChildViewController) — we've run into a couple of situations now where visible views were not firing recognizing gestures because their view controllers hadn't been added, and presumably the VCs themselves were not being retained.
It turned out that my gesture didn't work because it was in a normal class, and not a subclass of UIView or anything.
I solved the issue by creating a subclass of UIView that I had an instance of in this normal class and placed the gesture logic in that.
I solved my issue by setting the height to the UIView.
optionView.heightAnchor.constraint(equalToConstant: 18).isActive = true
I had the same issue in my programmatically created ViewController. The bug was fixed when I added a backgroundColor to the view.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
view.addGestureRecognizer(tap)
}
}

Resources