2016-12-16 12 views
2

iOSのGoogleマップ(Uberのような)の現在のユーザーの場所として、マーカー周りにパルスリングアニメーションを追加したいと考えています。私はaddAnimationによってマーカー層にCABasicAnimationを追加しようとしました。それは動作していません。Googleマップのマーカー周辺のパルスリングアニメーションiOS

また、私はマーカーのスケールをアニメーションしようとしましたが、スケールの変化は起こりませんでした。誰でもこのことで私を助けることができますか?

+1

ポストそれがどのように動作するかについての情報と一緒にあなたの現在のコード。 "それは働いていない"非常に有益ではありません。 –

+0

@DuncanCコード私は今答えとして更新しました。 ありがとうございました! –

答えて

12

何とか今働いています。カスタムビューを作成し、そのビューをGMSMarkerに設定しました。アイコンビュー。その後、ビューレイヤーにアニメーションを追加しました。

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)]; 
view.backgroundColor = [UIColor redColor]; 
view.layer.cornerRadius = 50; 

GMSMarker *m = [GMSMarker markerWithPosition:mapView_.myLocation.coordinate]; 
m.iconView = view; 
m.map = mapView_; 


CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
scaleAnimation.duration = 1.5; 
scaleAnimation.repeatCount = HUGE_VAL; 
scaleAnimation.autoreverses = YES; 
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.1]; 
scaleAnimation.toValue = [NSNumber numberWithFloat:1.2]; 

[view.layer addAnimation:scaleAnimation forKey:@"scale"]; 

別の方法:


GMSMarker *m = [GMSMarker markerWithPosition:mapView_.myLocation.coordinate]; 

//custom marker image 
    UIImageView *pulseRingImg = [[UIImageView alloc] initWithFrame: CGRectMake(-30, -30, 78, 78)]; 
    pulseRingImg.image = [UIImage imageNamed:@"Pulse"]; 
    pulseRingImg.userInteractionEnabled = NO; 


    //transform scale animation 
    CABasicAnimation *theAnimation; 
    theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"]; 
    theAnimation.duration = 3.5; 
    theAnimation.repeatCount = HUGE_VALF; 
    theAnimation.autoreverses = NO; 
    theAnimation.fromValue = [NSNumber numberWithFloat:0.0]; 
    theAnimation.toValue = [NSNumber numberWithFloat:2.0]; 

//alpha Animation for the image 
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; 
    animation.duration = 3.5; 
    animation.repeatCount = HUGE_VALF; 
    animation.values = [NSArray arrayWithObjects: 
         [NSNumber numberWithFloat:1.0], 
         [NSNumber numberWithFloat:0.5], 
         [NSNumber numberWithFloat:0.0], nil]; 
    animation.keyTimes = [NSArray arrayWithObjects: 
         [NSNumber numberWithFloat:0.0], 
         [NSNumber numberWithFloat:1.2], 
         [NSNumber numberWithFloat:3.5], nil]; 
    [pulseRingImg.layer addAnimation:animation forKey:@"opacity"]; 


    [pulseRingImg.layer addAnimation:theAnimation forKey:@"pulse"]; 
    pulseRingImg.userInteractionEnabled = NO; 

    m.iconView = pulseRingImg; 
    [m.layer addSublayer:pulseRingImg.layer]; 
    m.map = mapView_; 
    m.groundAnchor = CGPointMake(0.5, 0.5); 

もう一つ:

m = [GMSMarker markerWithPosition:mapView_.myLocation.coordinate]; 

    //custom marker image 
    UIImageView *pulseRingImg = [[UIImageView alloc] initWithFrame: CGRectMake(-30, -30, 78, 78)]; 
    pulseRingImg.image = [UIImage imageNamed:@"Pulse"]; 
    pulseRingImg.userInteractionEnabled = NO; 

    [CATransaction begin]; 
    [CATransaction setAnimationDuration:3.5]; 

    //transform scale animation 
    CABasicAnimation *theAnimation; 
    theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"]; 
    theAnimation.repeatCount = HUGE_VALF; 
    theAnimation.autoreverses = NO; 
    theAnimation.fromValue = [NSNumber numberWithFloat:0.0]; 
    theAnimation.toValue = [NSNumber numberWithFloat:2.0]; 

    [pulseRingImg.layer addAnimation:theAnimation forKey:@"pulse"]; 
    pulseRingImg.userInteractionEnabled = NO; 

    [CATransaction setCompletionBlock:^{ 
     //alpha Animation for the image 
     CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; 
     animation.duration = 3.8; 
     animation.repeatCount = HUGE_VALF; 
     animation.values = [NSArray arrayWithObjects: 
          [NSNumber numberWithFloat:1.0], 
          [NSNumber numberWithFloat:0.0], nil]; 
     [m.iconView.layer addAnimation:animation forKey:@"opacity"]; 
    }]; 

    [CATransaction commit]; 

    m.iconView = pulseRingImg; 
    [m.layer addSublayer:pulseRingImg.layer]; 
    m.map = mapView_; 
    m.groundAnchor = CGPointMake(0.5, 0.5); 

スイフト3.0コードは NOTEを下回っている:あなたの条件に基づいて所要時間を変更

  let m = GMSMarker(position: camera.target) 

      //custom marker image 
      let pulseRingImg = UIImageView(frame: CGRect(x: -30, y: -30, width: 78, height: 78)) 
      pulseRingImg.image = UIImage(named: "Pulse") 
      pulseRingImg.isUserInteractionEnabled = false 
      CATransaction.begin() 
      CATransaction.setAnimationDuration(3.5) 

      //transform scale animation 
      var theAnimation: CABasicAnimation? 
      theAnimation = CABasicAnimation(keyPath: "transform.scale.xy") 
      theAnimation?.repeatCount = Float.infinity 
      theAnimation?.autoreverses = false 
      theAnimation?.fromValue = Float(0.0) 
      theAnimation?.toValue = Float(2.0) 
      theAnimation?.isRemovedOnCompletion = false 

      pulseRingImg.layer.add(theAnimation!, forKey: "pulse") 
      pulseRingImg.isUserInteractionEnabled = false 
      CATransaction.setCompletionBlock({() -> Void in 

       //alpha Animation for the image 
       let animation = CAKeyframeAnimation(keyPath: "opacity") 
       animation.duration = 3.5 
       animation.repeatCount = Float.infinity 
       animation.values = [Float(2.0), Float(0.0)] 
       m.iconView?.layer.add(animation, forKey: "opacity") 
      }) 

      CATransaction.commit() 
      m.iconView = pulseRingImg 
      m.layer.addSublayer(pulseRingImg.layer) 
      m.map = gmapView 
      m.groundAnchor = CGPoint(x: 0.5, y: 0.5) 

パルスイメージ: pulse image for animation

+2

解決策を見つけられたらうれしいです。コードをモジュラ化するために、パルシングビューをGMSMarkerのカスタムサブクラスにすることについて考えるかもしれません。それはそれを落とし、より再利用可能にするでしょう。 (そして、あなたがそうすることができるようになると、答えを受け入れるべきです。) –

+0

@Duncan C、ありがとう!はい、再利用可能なように実装する必要があります。あなたがサンプルコードで私を助けることができれば、それはほとんど歓迎です。 –

+0

コードをすぐに3に変換できますか? –

関連する問題