简体   繁体   中英

iOS Google maps method UIView animate not working

So I would like to hide and show a button on my map that zooms in on the current user's location.

Basically when the map is no longer centred on the user's location, the button appears and disappears when it has just been pressed.

The button animates away when pressed but doesn't animate in when the method gets called

I am calling a method from Google map's api delegate that gets called every time the map moves.

Here is my code:

func showUserLocation() {
    mapView.isMyLocationEnabled = true
    self.locationManager.startUpdatingLocation()

    let userLocation = mapView.myLocation

    if let loc = userLocation
    {
        centreMapOnLocation(location: loc)
        if myLocationButton.alpha == 1 {
            UIView.animate(withDuration: 0.4, animations: {
                self.myLocationButton.alpha = 0
            })
        }
    }
}

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
    if myLocationButton.alpha == 0 {
        UIView.animate(withDuration: 0.4, animations: {
            self.myLocationButton.alpha = 1
        })
    }
}

When the willMove method gets called, it just appears but without the animation.

In the UIView.animate block add this line:

self.view.layoutIfNeeded()

with view being the containing view of the button you're trying to animate. This tells the view to redraw during the view's animation cycles.

try using for show button

self.myLocationButton.isHidden = false

and to hide button

self.myLocationButton.isHidden = true

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM