How to make a method available in the whole project - ios

I have the following code to handle a swipe gesture.
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Right) {
print("Swipe Right")
self.performSegueWithIdentifier("eventsModally", sender: self)
}
}
What is the best practise to make this method available in the whole project, instead of implementing it in every ViewController class?
Help is very appreciated.

Put it in a class extension.
extension UIViewController {
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Right) {
print("Swipe Right")
self.performSegueWithIdentifier("eventsModally", sender: self)
}
}
}

Related

How to send Swipe Action to UIView in Swift

I am trying to clone Tinder, have done with swipe left (dislike person) and right (like person) by Yalantis/Koloda (https://github.com/Yalantis/Koloda). I also have two images for button like/dislike. I wish my mainView will swipe Left/Right when I touch like/dislike image.
let likeImageTap = UITapGestureRecognizer(target: self, action: #selector(HomeViewController.sendSwipeRightAction))
likeImage.isUserInteractionEnabled = true
likeImage.addGestureRecognizer(likeImageTap)
How should I put in this func?
#objc func sendSwipeRightAction() {
}
This is my protocols
extension HomeViewController: KolodaViewDataSource, KolodaViewDelegate {
//...
func koloda(_ koloda: KolodaView, didSwipeCardAt index: Int, in direction: SwipeResultDirection)
{
if direction == .left {
//...
print ("swipe left")
}
if direction == .right {
//...
print("swipe right")
}
}
}
This is my view
#IBOutlet var mainView: KolodaView!
I try to use this func, then call it when image tapped, so I can do what I want same as swipe, but the view not change to next image.
public func swipe(_ direction: SwipeResultDirection, force: Bool = false)
{
//..
}
So I want to send swipe action to the view, example:
mainView.sendActions(for: .swipeLeft)
How should I do? Thanks for your suggestion.
There is code in example
kolodaView.swipe(.left)
kolodaView.swipe(.right)
your code will be like this
#objc func sendSwipeRightAction() {
mainView.swipe(.right)
}

How to segue when a button is held in a TableView cell

I have a pretty complex problem where the button already has a tap action as an #objc function. I likely have to implement code to the cellForRow function but how would it only call when these functions are at play?
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
//Was trying to figure out how to add segue call here
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 0.75
self.expandButton.addGestureRecognizer(longPress)
}
you could add a segue in your storyboard from the ViewController the table is in to the View you want to go to. Then give it an identifier and call func performSegue(withIdentifier identifier: String, sender: Any?)
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
performSegue(withIdentifier: String "showFromLongPress", sender: nil)
}
}
Here of course replace the identifier showFromLongPress with the identifier you specified in the storyboard.
Apple Developer Documentation

Change view controller using swipe gesture

I want to use swipe gesture to move to another view controller using right direction. Can you help me?
#IBAction func right(_ sender: UISwipeGestureRecognizer) {
// what I must write here
}
Just write one line code -
#IBAction func right(_ sender: UISwipeGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
self.performSegueWithIdentifier("nextView", sender: self)
break
default:
break
}
}
}

Show the copy menu on a view

I am trying to show the ios copy button on a view and handle the click on a custom function. I tried to display the button with this code but nothing is appearing.
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(paragraphTableViewCell.bounds, in: paragraphTableViewCell)
menu.setMenuVisible(true, animated: true)
}
EDIT:
I got this errors
You need to call canBecomeFirstResponder In your class.
And override canPerformAction then add your appropriate option as UIMenuItem
func canBecomeFirstResponder() -> Bool {
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any) -> Bool {
if action == #selector(self.cut) {
return false
}
else if action == #selector(self.copy) {
return true
}
else if action == #selector(self.paste) {
return false
}
else if action == #selector(self.select) || action == #selector(self.selectAll) {
return true
}
else {
return super.canPerformAction(action, withSender: sender)
}
}
override func copy(_ sender: Any?) {
}
Finally you should pass UIView object
menu.setTargetRect(paragraphTableViewCell.bounds, in: paragraphTableViewCell.contentView)
Because its required
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;

Recognize which View calls the Tap function

I know how to add tappability to the UIImageView, however, there are 2 image views and I want to distinguish them to call the correct function. However, I can't seem to get the correct sender.
func addTappability (view imageView:UIImageView){
//add tapping function for image
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(IdCardViewController.imageTapped(_:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
func imageTapped(_ sender: UIImageView) {
//Problem here, can't get correct sender
if ( sender == photoImageViewLeft) {
//do one thing
}else {
//do the other
}
}
Replace your function with this:
func imageTapped(_ sender: UITapGestureRecognizer) {
if let imageView = sender.view as? UIImageView {
if ( imageView == photoImageViewLeft ) {
print("Image1 Tapped")
}else {
print("Image2 Tapped")
}
}
}
You need to add tag where you're adding imageViews either in storyboard or in code.
then in your imageTapped() method compare them -
func imageTapped(_ sender: UIImageView) {
//Problem here, can't get correct sender
if ( sender.tag == 1) {
//do one thing
}else if(sender.tag ==2){
//do the other
}
}

Resources