我可以在GoogleMaps SDK for iOS中为比例标记设置动画吗?

时间:2014-05-08 10:33:12

标签: ios google-maps-sdk-ios

请参阅Do marker animations exist on GoogleMaps SDK for iOS?Google Dev Guide我了解我只能使用appearAnimation制作动画标记并通过GMSMarkerLayer动画不透明度

由于GMSMarkerLayer.iconUIImage。我可以动画缩放GMSMarker图标,就像名为Find My Friends的应用一样吗? (点击标记时,它会缩放比例或缩放标记图标)

谢谢。

2 个答案:

答案 0 :(得分:0)

您可以通过更改分配给它的ui图像来缩放gmsMarker图标。

理想情况下,您应该从ui线程回调动画函数(在代码片段中通过另一个线程完成)以防止闪烁。

以下是测试概念的示例代码段:

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;
}

}

答案 1 :(得分:0)

如果您希望动画标记在点击后生长而不是有弹性动画,请按以下方式初始化您的标记图像:

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

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

然后使用@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
}