使用ios 7视差效果移动图像

使用ios 7视差效果移动图像,ios,facebook,ios7,Ios,Facebook,Ios7,我刚刚看到Facebook的新纸质应用程序,它可以根据视差效果移动图像。所以它会将图像缩放到全屏,当你倾斜屏幕时,它会将图像滚动到你倾斜的一侧。我能够像苹果那样添加视差效果,但不像facebook那样。有人知道他们是怎么做到的吗。以下是我用于视差的基本代码: UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" t

我刚刚看到Facebook的新纸质应用程序,它可以根据视差效果移动图像。所以它会将图像缩放到全屏,当你倾斜屏幕时,它会将图像滚动到你倾斜的一侧。我能够像苹果那样添加视差效果,但不像facebook那样。有人知道他们是怎么做到的吗。以下是我用于视差的基本代码:

UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
interpolationHorizontal.minimumRelativeValue = @-10.0;
interpolationHorizontal.maximumRelativeValue = @10.0;

UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
interpolationVertical.minimumRelativeValue = @-10.0;
interpolationVertical.maximumRelativeValue = @10.0;

[self.backgroundView addMotionEffect:interpolationHorizontal];
[self.backgroundView addMotionEffect:interpolationVertical];
更新: 刚刚发现了一个非常好的第三方库来实现这一点,它叫做CRMotionView,它工作非常顺利,你可以修改很多东西

以下是github链接:

==================================================================================

当我第一次看到Facebook纸质应用程序时,我也在想同样的视差。但在玩了我的代码一点之后,我不认为视差是我们想要的。我可能错了,但我是从基地开始做的,陀螺仪运动管理器。以下是我的示例代码:

     //import the motion manager frame work first
     #import <CoreMotion/CoreMotion.h>

     //then need to add a motionManager
     @property (strong, nonatomic) CMMotionManager *motionManager;

    //you can paste all those codes in view did load
    //i added a scroll view on the view controller nib file
    self.mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    //we don't want it to bounce at each end of the image
    self.mainScrollView.bounces = NO;

    //and we don't want to allow user scrolling in this case
    self.mainScrollView.userInteractionEnabled = NO;

    //set up the image view
    UIImage *image= [UIImage imageNamed:@"YOUR_IMAGE_NAME"];
    UIImageView *movingImageView = [[UIImageView alloc]initWithImage:image];
    [self.mainScrollView addSubview:movingImageView];

    //set up the content size based on the image size
    //in facebook paper case, vertical rotation doesn't do anything
    //so we dont have to set up the content size height
    self.mainScrollView.contentSize = CGSizeMake(movingImageView.frame.size.width, self.mainScrollView.frame.size.height);

    //center the image at intial
    self.mainScrollView.contentOffset = CGPointMake((self.mainScrollView.contentSize.width - self.view.frame.size.width) / 2, 0);

    //inital the motionManager and detec the Gyroscrope for every 1/60 second
    //the interval may not need to be that fast
    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.gyroUpdateInterval = 1/60;

    //this is how fast the image should move when rotate the device, the larger the number, the less the roation required.
    CGFloat motionMovingRate = 4;

    //get the max and min offset x value
    int maxXOffset = self.mainScrollView.contentSize.width - self.mainScrollView.frame.size.width;
    int minXOffset = 0;

    [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
                                withHandler:^(CMGyroData *gyroData, NSError *error) {
         //since our hands are not prefectly steady
         //so it will always have small rotation rate between 0.01 - 0.05
         //i am ignoring if the rotation rate is less then 0.1
         //if you want this to be more sensitive, lower the value here
         if (fabs(gyroData.rotationRate.y) >= 0.1) {
            CGFloat targetX = self.mainScrollView.contentOffset.x - gyroData.rotationRate.y * motionMovingRate;
             //check if the target x is less than min or larger than max
             //if do, use min or max
             if(targetX > maxXOffset)
                   targetX = maxXOffset;
             else if (targetX < minXOffset)
                   targetX = minXOffset;

             //set up the content off
             self.mainScrollView.contentOffset = CGPointMake(targetX, 0);
          }
   }];
//首先导入运动管理器框架
#进口
//然后需要添加一个motionManager
@属性(强、非原子)CMMotionManager*motionManager;
//您可以粘贴视图中的所有代码
//我在视图控制器nib文件上添加了一个滚动视图
self.mainsrollview.frame=CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
//我们不希望它在图像的每一端反弹
self.mainsrollview.bounces=否;
//在这种情况下,我们不希望允许用户滚动
self.mainScrollView.userInteractionEnabled=否;
//设置图像视图
UIImage*image=[UIImage ImageName:@“您的图像名称”];
UIImageView*movingImageView=[[UIImageView alloc]initWithImage:image];
[self.main滚动视图添加子视图:移动图像视图];
//根据图像大小设置内容大小
//在facebook的案例中,垂直旋转没有任何作用
//因此,我们不必设置内容大小高度
self.mainScrollView.contentSize=CGSizeMake(movingImageView.frame.size.width,self.mainScrollView.frame.size.height);
//将图像居中放置在初始位置
self.mainScrollView.contentOffset=CGPointMake((self.mainScrollView.contentSize.width-self.view.frame.size.width)/2,0);
//初始化motionManager并每隔1/60秒检测一次Gyroscrope
//间隔可能不需要那么快
self.motionManager=[[CMMotionManager alloc]init];
self.motionManager.gyroUpdateInterval=1/60;
//这是旋转设备时图像移动的速度,数字越大,所需的移动越少。
CGFloat-movingrate=4;
//获取最大和最小偏移x值
int maxoxoffset=self.maincollview.contentSize.width-self.maincollview.frame.size.width;
int minXOffset=0;
[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMGyroData*gyroData,N错误*错误){
//因为我们的手不太稳
//因此,在0.01-0.05之间,它始终具有较小的旋转速率
//如果旋转速率小于0.1,我将忽略
//如果希望更敏感,请降低此处的值
if(fabs(回转数据旋转率y)>=0.1){
CGFloat targetX=self.mainScrollView.contentOffset.x-gyroData.rotationRate.y*motionMovingRate;
//检查目标x是否小于最小值或大于最大值
//如果需要,则使用最小值或最大值
如果(targetX>maxXOffset)
targetX=maxXOffset;
否则如果(targetX
我在我的设备上进行了测试,工作原理与facebook的新应用程序非常相似


不过,这只是我在半小时内编写的一个示例代码,可能不是100%准确,但希望这能给您一些想法。

谢谢您的回答,但当您将设备向右或向左旋转时会发生这种情况,上下如何?我的意思是我们可以看到图像的各个角落,你知道吗?@Mc.Lover嗨,我在度假,一点也没有检查堆栈溢出,我想这对上下都是绝对可行的,让我尝试一下并编辑我的答案。它运行得很好,但在控制台中会抛出错误:
:ImageIO:CreateMetadataFromXMPBufferInternal抛出错误#201(XML解析失败)