iOS開発-CoreMotionフレーム(加速計とジャイロスコープ)

2001 ワード

http://www.cnblogs.com/xiaofeixiang/p/4317151.html 例:大画面が表示されきれない場合、左右のオフセット表示原理:ジャイロスコープを使ってy軸のオフセット変化をモニターする。

-(void)startAnimate
{
    //        
    float scrollSpeed = (_myImageView.frame.size.width - self.frame.size.width)/2/SPEED;
    [WJGravity sharedGravity].timeInterval = 0.05;
    
    [[WJGravity sharedGravity]startDeviceMotionUpdatesBlock:^(float x, float y, float z) {
        NSLog(@"%f,%f,%f",x,y,z);
        [UIView animateKeyframesWithDuration:0.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeDiscrete animations:^{
            
            //         
            if (_myImageView.frame.origin.x <=0 && _myImageView.frame.origin.x >= self.frame.size.width - _myImageView.frame.size.width)
            {
                float invertedYRotationRate = y * -1.0;
                
                //    y    
                
                //_myImageView       +     
                float interpretedXOffset = _myImageView.center.x + invertedYRotationRate * (_myImageView.frame.size.width/[UIScreen mainScreen].bounds.size.width) * scrollSpeed;
                
                //       
                _myImageView.center = CGPointMake(interpretedXOffset, _myImageView.center.y);
            }
            
            //       ,      
            if (_myImageView.frame.origin.x >0)
            {
                _myImageView.frame = CGRectMake(0, _myImageView.frame.origin.y, _myImageView.frame.size.width, _myImageView.frame.size.height);
            }
            //       ,      
            if (_myImageView.frame.origin.x < self.frame.size.width - _myImageView.frame.size.width)
            {
                _myImageView.frame = CGRectMake(self.frame.size.width - _myImageView.frame.size.width, _myImageView.frame.origin.y, _myImageView.frame.size.width, _myImageView.frame.size.height);
            }
        } completion:nil];
        
        
    }];
}
デモ接続:https://github.com/WuJiForFantasy/WJGravity