I get the following error in the LongPressViewController
class.
import UIKit
class LongPressViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
@IBAction func btnTekan(_ sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
let alertController = UIAlertController(title: nil, message: "Long Press terdeteksi", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
}
@objc func handleLongPress(_ recognizer: UILongPressGestureRecognizer){
switch recognizer.state {
case .began:
UIView.animate(withDuration: 0.05,
animations: {
self.imgView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
},
completion: nil)
case .ended:
UIView.animate(withDuration: 0.05) {
self.imgView.transform = CGAffineTransform.identity
}
default:
break
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You should indicate what error is reported on which line of your code.
As is, your code may warn you that the constant longPressGesture
declared and initialized in viewDidLoad()
was never used. This is not an error, but just a friendly reminder that this line has no effect. You may attach the recognizer to the controlled view by adding the following line:
view.addGestureRecognizer(longPressGesture)
Alternatively, you can declare longPressGesture
as an @IBOutlet
and attach it in Interface Builder.
Further up where you use State
, the property state
implies already the type UIGestureRecognizer.State
, so you may shorten your line to
if sender.state == .began {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.