美文网首页iOS动画&绘图
iOS开发-CoreMotion框架(加速计和陀螺仪)

iOS开发-CoreMotion框架(加速计和陀螺仪)

作者: 幻想无极 | 来源:发表于2016-08-09 11:13 被阅读77次

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

    demo连接:https://github.com/WuJiForFantasy/WJGravity

    相关文章

      网友评论

        本文标题:iOS开发-CoreMotion框架(加速计和陀螺仪)

        本文链接:https://www.haomeiwen.com/subject/lmfdsttx.html