I am developing a map-based app where user can select map annotations and then a UIView must appear with a photo from database but I press on annotation nothing happens
Here is the code
import UIKit
import MapKit
import CoreLocation
import Firebase
import FirebaseStorage
class MapViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var MapButton: UITabBarItem!
@IBOutlet weak var mapView: MKMapView!
> Here is the UIView that must appear after user selected an annotation
@IBOutlet var addView: UIView!
@IBOutlet weak var URLImageView: UIImageView!
override func viewDidLoad() {
addView.layer.cornerRadius = 5
}
func animateIn() {
self.view.addSubview(addView)
addView.center = self.view.center
addView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
addView.alpha = 0
UIView.animate(withDuration: 0.3) {
self.addView.alpha = 1
self.addView.transform = CGAffineTransform.identity
}
}
func animateOut() {
UIView.animate(withDuration: 0.3) {
self.addView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
self.addView.alpha = 0
} completion: { (success:Bool) in
self.addView.removeFromSuperview()
}
}
Here is the method that activates the methods when user selects an annotation. It seeks the image in the database and retrieves it, I tried that with a simple UIInageView and it worked awesome, it retrieved the image and put it into the image view but I needed to customize it so I created a custom UIView that had to appear when the user presses on annotation
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
var coord = self.selectedAnnotation
coord = view.annotation as? MKPointAnnotation
let codeLat = coord?.coordinate.latitude
let codeLon = coord?.coordinate.longitude
animateIn()
let db = Firestore.firestore()
db.collection("locations").getDocuments() { [self] (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
if let coords = document.get("pinLocation") {
let point = coords as! GeoPoint
let lat = point.latitude
let lon = point.longitude
if codeLat == lat && codeLon == lon {
let imageUrl = document.data()["url"]
print(imageUrl!)
let er = "not found"
let url = URL(string: "\(imageUrl ?? er)")!
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url) {
DispatchQueue.main.async {
self.ecoURLImageView.image = UIImage(data: data)
URLImageView.layer.cornerRadius = 10
URLImageView.clipsToBounds = true
URLImageView.layer.borderColor = UIColor.systemGreen.cgColor
URLImageView.layer.borderWidth = 3
}
}
}
}
}
}
}
}
}
It looks like you havent set a frame for your view.
try
self.myView.frame = CGRect(X:0,y:0,width:100,height:100)
self.view.addSubview(myView)
If you've added a UI element in Storyboard, you do not want to be re-adding it to or removing it from the view hierarchy.
Try it like this:
class MapViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var MapButton: UITabBarItem!
@IBOutlet weak var mapView: MKMapView!
// > Here is the UIView that must appear after user selected an annotation
@IBOutlet var addView: UIView!
@IBOutlet weak var URLImageView: UIImageView!
override func viewDidLoad() {
addView.layer.cornerRadius = 5
// hide addView on load
self.addView.isHidden = true
}
func animateIn() {
// un-hide addView
self.addView.isHidden = false
// it's already in the view hierarchy - so don't add it!!!
//self.addView.removeFromSuperview()
//self.view.addSubview(addView)
addView.center = self.view.center
addView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
addView.alpha = 0
UIView.animate(withDuration: 0.3) {
self.addView.alpha = 1
self.addView.transform = CGAffineTransform.identity
}
}
func animateOut() {
UIView.animate(withDuration: 0.3) {
self.addView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
self.addView.alpha = 0
} completion: { (success:Bool) in
// hide addView
self.addView.isHidden = true
// don't remove it from superview
//self.addView.removeFromSuperview()
}
}
}
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.