如果用户按住或释放视图,我希望进度条增加或减少。现在,我只在View上使用UILongPressGestureRecognizer,但现在不怎么按住View来激活进度条。我的程序在代码中被命名为counterView。 有人可以帮我吗?
这是我现在的代码:
class Main: UIViewController {
@IBOutlet weak var myView: UIView!
@IBOutlet weak var counterView: CounterView!
var timer: Timer!
var progressCounter:Float = 0
let duration:Float = 3.0
var progressIncrement:Float = 0
override func viewDidLoad() {
super.viewDidLoad()
let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))
myView.addGestureRecognizer(tap)
}
@objc func showProgress() {
if(progressCounter > 1.0){timer.invalidate()}
counterView.progress = progressCounter
progressCounter = progressCounter + progressIncrement
}
@objc func tapHandler(gesture: UILongPressGestureRecognizer) {
// handle touch down and touch up events separately
if gesture.state == .began {
addView()
progressIncrement = 1.0/duration
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.showProgress), userInfo: nil, repeats: true)
}
else if gesture.state == .ended {
addFlashView()
}
}
func addView() {
let label = UILabel(frame: CGRect(x: counterView.frame.size.width / 2, y: counterView.frame.size.height / 2, width: 230, height: 20))
label.textColor = .green
label.center = CGPoint(x: counterView.frame.size.width / 2, y: counterView.frame.size.height / 2)
label.textAlignment = .center
label.text = "Hold down"
myView.backgroundColor = UIColor.lightGray
counterView.isHidden=false
counterView.addSubview(label)
}
func addFlashView(){
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewController(withIdentifier: "ResultView") as! Flash
self.present(resultViewController, animated:true, completion:nil)
}
}