1

I'm trying to design an interface where a tap fades text in, then fades it out, but dragging two fingers up on the screen makes it brighter, and dragging two fingers down make the screen dimmer. I've gotten the text to fade in and out, but I can't seem to get the brightness functionality to work. The app is building, but the two finger gesture does nothing at all.

I have tried inserting code found here.

I have tried several other methods found on Stack Overflow, such as those found here as well, but with no luck.

UPDATE: I made some changes based off of feedback in the comments from @rmaddy and @leo-dabus. There's still nothing at all happening when I pan in the simulator or on my iPhone. I'm not sure if I should be using "recognizer.state" instead of "sender.state". I'm sure I'm making lots of beginner mistakes. Do I even have the code for the pan gesture in the right place? Here's what I have now:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        // Hide text on launch
        self.text.alpha = 0

    }
    // Introducing: The text!
    @IBOutlet weak var text: UITextView!

    // Show text upon tap of gesture recognizer
    @IBAction func tap(_ sender: UITapGestureRecognizer) {
            // fade in
            UIView.animate(withDuration: 0.5, animations: {
                self.text.alpha = 1.0
            }) { (finished) in
                // fade out
                UIView.animate(withDuration: 4.0, animations: {
                    self.text.alpha = 0.0
                })
            }
        }


    @IBAction func twoFingerPan(_ sender: UIPanGestureRecognizer) {
        if sender.state == UIPanGestureRecognizer.State.changed || sender.state == UIPanGestureRecognizer.State.ended {
            let velocity:CGPoint = sender.velocity(in: self.view)
            if velocity.y > 0 {
                UIScreen.main.brightness -= 0.03
            }
            else {
                UIScreen.main.brightness += 0.03
                }
            }
        }
    }
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Why is the `pan` function inside the `TwoFingerPan` function? – rmaddy May 04 '19 at 21:45
  • 2
    FYI - it is standard practice that class, struct, and enum names start with uppercase letters. Variable, function, and case names start with lowercase letters. – rmaddy May 04 '19 at 21:46
  • And brightness is a CGFloat type why are you coercing it to Float and then back to CGFloat? Just declare your brightness object as CGFloat as well – Leo Dabus May 05 '19 at 01:06
  • Thanks for the advice on the uppercase/lowercase best practices. The pan is inside of the TwoFingerPan function because I drag/dropped my Pan Gesture Recognizer into the viewcontroller.swift, then pasted and adapted the code I found online to do the brightness control underneath it. If I remove the TwoFingerPan function, the build succeeds but pan doesn't work. If I remove the second pan function, the build fails with a "Use of unresolved identifier 'recognizer'" error. As for the CGFloat type question, I don't know. I'm very much new to this and appreciate the questions and feedback! – mozltovcoktail May 05 '19 at 01:27
  • you can replace those 3 lines with a single one `UIScreen.main.brightness -= 0.03` – Leo Dabus May 05 '19 at 02:57
  • @LeoDabus I updated the original post with some revised code based on your feedback. It still does nothing, but it looks cleaner! – mozltovcoktail May 05 '19 at 13:45
  • Again rename your methods, variables and ibactions (lowerCamelCase) – Leo Dabus May 05 '19 at 13:48
  • @LeoDabus I did change "TwoFingerPan" "twoFingerPan," and in my next version I'll call "Tap" "tap". Everything else was generated by Xcode. Should I rename what Xcode generated as well? Any idea why my code isn't achieving the desired result? – mozltovcoktail May 05 '19 at 22:20
  • @mozltovcoktail edit your question, post your actual code and remove the old one – Leo Dabus May 05 '19 at 22:27
  • @LeoDabus Done. – mozltovcoktail May 06 '19 at 02:31
  • Where did are you add the gesture recognizer ? – Leo Dabus May 06 '19 at 02:44
  • @LeoDabus I added both the tap and pan gesture recognizers through the storyboard instead of adding them through code in ViewController.swift. Since the tap gesture worked successfully doing it that way, I thought that pan would work the same way. Am I mistaken? – mozltovcoktail May 06 '19 at 12:02
  • I'm seeing conflicting info on online tutorials about if I need to introduce the gesture recognizers outside of the @IBAction line, and whether to use sender or recognizer, so some clarity here would be great! – mozltovcoktail May 06 '19 at 12:52

1 Answers1

0

I suggest creating a couple of gesture recognizers. Something like this:

fileprivate func setGestureRecognizers() {
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
    tapGesture.delegate = self as? UIGestureRecognizerDelegate
    tapGesture.numberOfTapsRequired = 1
    view.addGestureRecognizer(tapGesture)

    let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(twoFingerPan(_:)))
    panGesture.delegate = self as? UIGestureRecognizerDelegate
    panGesture.minimumNumberOfTouches = 1
    view.addGestureRecognizer(panGesture)
}

On viewDidLoad call this method:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    // Hide text on launch
    self.text.alpha = 0

    setGestureRecognizers()
}

Then I change a little your methods because I set the gesture recognizers using code and not storyboards, so I got rid of @IBAction and added @objc. Also I was testing this on my phone and comparing velocity.y > 0 made the gesture too sensitive to finger movement, so I changed it to 0.4 instead.

// Show text upon tap of gesture recognizer
@objc func tap(_ sender: UITapGestureRecognizer) {
    // fade in
    UIView.animate(withDuration: 0.5, animations: {
        self.text.alpha = 1.0
    }) { (finished) in
        // fade out
        UIView.animate(withDuration: 4.0, animations: {
            self.text.alpha = 0.0
        })
    }
}

@objc func twoFingerPan(_ sender: UIPanGestureRecognizer) {
    if sender.state == UIPanGestureRecognizer.State.changed || sender.state == UIPanGestureRecognizer.State.ended {
        let velocity:CGPoint = sender.velocity(in: self.view)
        if velocity.y > 0.4 {
            UIScreen.main.brightness -= CGFloat(0.03)
        }
        else {
            UIScreen.main.brightness += CGFloat(0.03)
        }
    }
}
Aaron Cyrman
  • 556
  • 3
  • 13
  • Thanks so much for your help! I got this working last week, but will still try out your advice, especially on the sensitivity issue. I was, of course, missing something obvious, which is that I hadn't control+dragged the pan gesture recognizer onto the view. – mozltovcoktail May 20 '19 at 12:21