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
}
}
}
}