如何修改Grant Pauls的Shimmer控件,以便屏幕响应加速度计输入?

时间:2015-02-25 12:20:30

标签: ios core-motion caanimation

如何修改Grant Pauls's cool Shimmer control以响应加速度计输入?我是iOS开发的新手,并且已经分离了部分代码,但我不确定如何“挂钩”,所以它响应加速度计输入,如下图所示:

(我在找到Shimmer控件之前制作了草图,这里的“White Stripe”将是控件的非遮罩层)

编辑:我不是在寻找完整的代码,而只是关于如何创建代码的想法。 CAAnimation和Core Motion对我来说是全新的。 “将x设为y的代表,并查看已发布代码的第z行”,例如,这将有资格作为我的答案。

enter image description here

以下是我认为参与“连接”加速度计的代码:

  BOOL disableActions = [CATransaction disableActions];
  if (!_shimmering) {
    if (disableActions) {
      // simply remove mask
      [self _clearMask];
    } else {
      // end slide
      CFTimeInterval slideEndTime = 0;

      CAAnimation *slideAnimation = [_maskLayer animationForKey:kFBShimmerSlideAnimationKey];
      if (slideAnimation != nil) {
        // determing total time sliding
        CFTimeInterval now = CACurrentMediaTime();
        CFTimeInterval slideTotalDuration = now - slideAnimation.beginTime;

        // determine time offset into current slide
        CFTimeInterval slideTimeOffset = fmod(slideTotalDuration, slideAnimation.duration);

        // transition to non-repeating slide
        CAAnimation *finishAnimation = shimmer_slide_finish(slideAnimation);

        // adjust begin time to now - offset
        finishAnimation.beginTime = now - slideTimeOffset;

        // note slide end time and begin
        slideEndTime = finishAnimation.beginTime + slideAnimation.duration;
        [_maskLayer addAnimation:finishAnimation forKey:kFBShimmerSlideAnimationKey];
      }

      // fade in text at slideEndTime
      CABasicAnimation *fadeInAnimation = shimmer_end_fade_animation(self, _maskLayer.fadeLayer, 1.0, _shimmeringEndFadeDuration);
      fadeInAnimation.beginTime = slideEndTime;
      [_maskLayer.fadeLayer addAnimation:fadeInAnimation forKey:kFBFadeAnimationKey];

      // expose end time for synchronization
      _shimmeringFadeTime = slideEndTime;
    }
  } else {
    // fade out text, optionally animated
    CABasicAnimation *fadeOutAnimation = nil;
    if (_shimmeringBeginFadeDuration > 0.0 && !disableActions) {
      fadeOutAnimation = shimmer_begin_fade_animation(self, _maskLayer.fadeLayer, 0.0, _shimmeringBeginFadeDuration);
      [_maskLayer.fadeLayer addAnimation:fadeOutAnimation forKey:kFBFadeAnimationKey];
    } else {
      BOOL innerDisableActions = [CATransaction disableActions];
      [CATransaction setDisableActions:YES];

      _maskLayer.fadeLayer.opacity = 0.0;
      [_maskLayer.fadeLayer removeAllAnimations];

      [CATransaction setDisableActions:innerDisableActions];
    }

    // begin slide animation
    CAAnimation *slideAnimation = [_maskLayer animationForKey:kFBShimmerSlideAnimationKey];

    // compute shimmer duration
    CGFloat length = 0.0f;
    if (_shimmeringDirection == FBShimmerDirectionDown ||
        _shimmeringDirection == FBShimmerDirectionUp) {
      length = CGRectGetHeight(_contentLayer.bounds);
    } else {
      length = CGRectGetWidth(_contentLayer.bounds);
    }
    CFTimeInterval animationDuration = (length / _shimmeringSpeed) + _shimmeringPauseDuration;

    if (slideAnimation != nil) {
      // ensure existing slide animation repeats
      [_maskLayer addAnimation:shimmer_slide_repeat(slideAnimation, animationDuration, _shimmeringDirection) forKey:kFBShimmerSlideAnimationKey];
    } else {
      // add slide animation
      slideAnimation = shimmer_slide_animation(self, animationDuration, _shimmeringDirection);
      slideAnimation.fillMode = kCAFillModeForwards;
      slideAnimation.removedOnCompletion = NO;
      slideAnimation.beginTime = CACurrentMediaTime() + fadeOutAnimation.duration;
      [_maskLayer addAnimation:slideAnimation forKey:kFBShimmerSlideAnimationKey];
    }
  }
}

1 个答案:

答案 0 :(得分:0)

当您 - 查看视图时,您需要创建一个类似的MotionManager:

    self.motionManager = [[CMMotionManager alloc] init];
    [self.motionManager startAccelerometerUpdates];

在该视图中的-dealloc上,你也想要摆脱它:

    [self.motionManager stopAccelerometerUpdates];
    self.motionManager = nil;

以下是对您最重要的部分,您需要向运动管理器查询设备方向的更新。我总是在更新方法中的SpriteKit场景的上下文中完成此操作,但我很确定你可以添加一个动作管理器委托(或其他东西)并完成同样的事情。重要的部分如下:

CMAccelerometerData* data = self.motionManager.accelerometerData;
CGFloat xacceleration = data.acceleration.x;
CGFloat yacceleration = data.acceleration.y;

使用该x / y加速度,您可以将其映射到视图中的某个点,并将闪光效果映射到该点。我希望有所帮助。