Niall Kiddle
Niall Kiddle

Reputation: 1487

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.

Upvotes: 0

Views: 844

Answers (2)

Tom E
Tom E

Reputation: 1607

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.

Upvotes: 3

Hafiz Shoaib Awan
Hafiz Shoaib Awan

Reputation: 302

try using for show button

self.myLocationButton.isHidden = false

and to hide button

self.myLocationButton.isHidden = true

Upvotes: 0

Related Questions