用ios 7视差效果移动图像

我刚刚看到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]; 

更新:刚刚find一个非常好的第三方库来实现这个,它叫做CRMotionView,工作非常stream畅,你可以修改很多东西。

这里是github链接: https : //github.com/chroman/CRMotionView

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

当我第一次看到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); } }]; 

我testing了这个在我的设备上,工作非常类似Facebook的新应用程序。

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