当前位置: 动力学知识库 > 问答 > 编程问答 >

google maps sdk ios - Can I animate scale marker in GoogleMaps SDK for iOS?

问题描述:

Refer to Do marker animations exist on GoogleMaps SDK for iOS? and Google Dev Guide I understand that I can only animate marker when it appear using appearAnimation and animate opacity via GMSMarkerLayer

Since GMSMarkerLayer.icon is UIImage. Can I animate Scale the GMSMarker icon like an app called Find My Friends? (When tap the marker, it animate scale or zoom the marker icon bigger)

Thank you.

网友答案:

You can scale the gmsMarker icon by changing the ui image assigned to it.

Ideally you should have the callback to the animation function from an ui thread (in the snippet is done via another thread) to prevent flickers.

Here's a sample code snippet to test the concept:

BOOL runInflateAnimation=YES;
float curScaleFactor=1.0f;
float curScaleDirection=-1.0f;
float scaleStep=0.2f;
float scaleMin=0.5f;
float scaleMax=2.0f;

double lastTimeStamp;
-(void) startBouncyAnimation
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,(unsigned   long)NULL), ^(void)
               {
                   while (runBouncyAnimation)
                   {

                       double curTimeStamp =[[NSDate date] timeIntervalSince1970];
                       double delta;
                       if (lastTimeStamp>0)
                       {
                           delta = curTimeStamp - lastTimeStamp;
                           curScaleFactor+=scaleStep*curScaleDirection * delta;
                           if (curScaleFactor<scaleMin)
                           {
                               curScaleFactor=scaleMin;
                               curScaleDirection*=-1.0f;
                           } else if (curScaleFactor>scaleMax)
                           {
                               curScaleFactor=scaleMax;
                               curScaleDirection*=-1.0f;
                           }
                           [self hackAnimation];

                       }
                       lastTimeStamp =curTimeStamp;
                       [NSThread sleepForTimeInterval:0.1f];
                   }

               }
               );
}


-(void) hackAnimation
{
    if (![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:@selector(hackAnimation) withObject:nil waitUntilDone:NO];
    }
@synchronized(currentLocationMarker)
{
GMSMarker* theMarker = currentLocationMarker;
UIImage* curImage = [UIImage imageNamed:@"myMarker.png"];
UIImage* scaled = [UIImage imageWithData:UIImagePNGRepresentation(curImage) scale:curScaleFactor];
theMarker.icon=scaled;
}

}
网友答案:

If you want to animate marker grow once it's tapped instead of having bouncy animation, init your marker image like this:

kStartingImageScale = 3.0 // make it available in your class.

marker.icon = UIImage(data: UIImagePNGRepresentation(UIImage(named: "marker")!)!, scale: CGFloat(kStartingImageScale))!

Then use a little modified method by @MichaelCMS

func startAnimation(shouldGrow:Bool) {

    let scaleMin:Double = 2.0;
    let scaleMax:Double = kStartingImageScale;
    var curScaleFactor:Double = shouldGrow ? scaleMax: scaleMin;
    var curScaleDirection:Double = 1.0;
    let scaleStep:Double = 4.0;

    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {

        var runAnimation: Bool = true

        var lastTimeStamp:Double?

        while (runAnimation)
        {

            let curTimeStamp:Double = NSDate().timeIntervalSince1970
            var delta:Double;
            if (lastTimeStamp>0)
            {
                delta = curTimeStamp - lastTimeStamp!;
                curScaleFactor+=scaleStep*curScaleDirection*delta;
                if (curScaleFactor < scaleMin)
                {
                    curScaleFactor = scaleMin;
                    curScaleDirection = curScaleDirection*(-1.0);

                    runAnimation = !shouldGrow

                }
                else if (curScaleFactor>scaleMax)
                {
                    curScaleFactor = scaleMax;
                    curScaleDirection = curScaleDirection*(-1.0);

                    runAnimation = shouldGrow

                }
                self.hackAnimation(curScaleFactor)

            }

            lastTimeStamp = curTimeStamp;
            NSThread.sleepForTimeInterval(0.01)
        }
    }
}

func hackAnimation(curScaleFactor:Double)
{
    let theMarker:GMSMarker = self.marker!;
    let curImage:UIImage = UIImage(named: "marker")!
    let scaled:UIImage  = UIImage(data: UIImagePNGRepresentation(curImage)!, scale: CGFloat(curScaleFactor))!

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        theMarker.icon=scaled;
    })
}

// MARK: - GMSMapViewDelegate

var grooow:Bool = true;

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {

    DDLogDebug("Marker tapped \(marker)")
    startAnimation(grooow)
    grooow = !grooow

    return true
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: