Нет причин для удаления / повторного добавления mView
. Все, что вам нужно сделать, это установить его .alpha
на ноль, изменить его кадр, а затем оживить .alpha
обратно на 1 (заставит его «исчезать».
// set alpha to Zero (invisible)
self.mView.alpha = 0.0
// set its new frame
self.mView.frame = CGRect(x: 0, y: 0, width: 121, height: 58)
// "fade in"
UIView.animate(withDuration: 1.0) {
self.mView.alpha = 1.0
}
Вот полный Пример. Каждый раз, когда вы нажимаете, красный mView
исчезает, а затем «постепенно появляется» в случайном месте:
class ViewController: UIViewController {
let mView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
mView.backgroundColor = .red
mView.frame = CGRect(x: 100, y: 200, width: 121, height: 58)
view.addSubview(mView)
let tap = UITapGestureRecognizer(target: self, action: #selector(self.doAnim))
view.addGestureRecognizer(tap)
}
@objc func doAnim() -> Void {
let x = CGFloat.random(in: 0...(view.frame.size.width - 121))
let y = CGFloat.random(in: 0...(view.frame.size.height - 58))
// set alpha to Zero (invisible)
self.mView.alpha = 0.0
// set its new frame
self.mView.frame = CGRect(x: x, y: y, width: 121, height: 58)
// "fade in"
UIView.animate(withDuration: 1.0) {
self.mView.alpha = 1.0
}
}
}
Изменить: исходный опубликованный вопрос не был фактическим вопросом
Вот ваш опубликованный код с парой изменений. Прочтите комментарии:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// (1) - if you are creating a NEW view at (3),
// remove this view first
//self.infoWindow.alpha = 0
self.infoWindow.removeFromSuperview()
// (2) - not needed
//self.view.layoutIfNeeded()
let data = marker.userData as! [String : Any]
let location = CLLocationCoordinate2D(latitude: (data["latitude"] as! Double), longitude: (data["longitude"] as! Double))
// (3) - create a NEW view
self.infoWindow = CustomGMSInfoWindow(frame: CGRect.init(x: 0, y: 0, width: 121, height: 58))
self.infoWindow.center = mapView.projection.point(for: location)
self.infoWindow.center.y = (infoWindow.center.y) - 65
// (4) - this is setting the frame to itself - you don't need this
self.infoWindow.frame = CGRect(infoWindow.frame.origin.x, infoWindow.frame.origin.y, infoWindow.frame.width, infoWindow.frame.height)
// (5) - you created a NEW view, so you need to set its alpha here
self.infoWindow.alpha = 0.0
// (6) - and you need to add it as a subview... I'm assuming it needs to be added to the mapView
mapView.addSubview(self.infoWindow)
// (7) if this doesn't fade in the new view...
UIView.animate(withDuration: 1.0) {
self.infoWindow.alpha = 1.0
}
// (8) try it like this - wait 5/100ths of a second before starting the fade-in
UIView.animate(withDuration: 1.0, delay: 0.05, animations: {
self.infoWindow.alpha = 1.0
})
return false
}