Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 7“我的视图”控制器中的视差效果_Ios_Objective C_Swift_Parallax - Fatal编程技术网

iOS 7“我的视图”控制器中的视差效果

iOS 7“我的视图”控制器中的视差效果,ios,objective-c,swift,parallax,Ios,Objective C,Swift,Parallax,我正在为Objective-C中的iOS 7开发一个应用程序。我的应用程序中有一个屏幕,有几个按钮和一个漂亮的背景图像。(这是一个简单的xib,在UIImageView上有ui按钮) 我在想,如果这些按钮具有iOS 7主屏幕所具有的视差效果,那就太酷了,所以如果你倾斜手机,你可以看到背景 如何在我自己的应用程序中实现这种效果?UIMotionEffect在iOS 7上提供了免费视差实现 您只需设置视差强度。在iOS 7中,苹果引入了与用户设备方向相关的运动效果。例如,要在主屏幕上模拟视差效果,

我正在为Objective-C中的iOS 7开发一个应用程序。我的应用程序中有一个屏幕,有几个按钮和一个漂亮的背景图像。(这是一个简单的xib,在
UIImageView
上有
ui按钮

我在想,如果这些按钮具有iOS 7主屏幕所具有的视差效果,那就太酷了,所以如果你倾斜手机,你可以看到背景


如何在我自己的应用程序中实现这种效果?

UIMotionEffect在iOS 7上提供了免费视差实现


您只需设置视差强度。

在iOS 7中,苹果引入了与用户设备方向相关的运动效果。例如,要在主屏幕上模拟视差效果,可以使用子类,如前所述,只需几行代码

目标-C

// Set vertical effect
UIInterpolatingMotionEffect *verticalMotionEffect = 
  [[UIInterpolatingMotionEffect alloc] 
  initWithKeyPath:@"center.y"
             type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-10);
verticalMotionEffect.maximumRelativeValue = @(10);

// Set horizontal effect 
UIInterpolatingMotionEffect *horizontalMotionEffect = 
  [[UIInterpolatingMotionEffect alloc] 
  initWithKeyPath:@"center.x"     
             type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-10);
horizontalMotionEffect.maximumRelativeValue = @(10);
  
// Create group to combine both
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];

// Add both effects to your view
[myBackgroundView addMotionEffect:group];
Swift(感谢@Lucas):

此外,您还可以找到一组库来简化此操作,或将此功能添加到较旧的iOS版本:

  • (需要iOS 7
  • (需要iOS 5.0或更高版本和ARC
  • (使用iOS 6.0进行测试,需要ARC
  • (使用iOS 6.1进行测试,需要ARC

以下是一个简单的类别,可用于整合对iOs7+的影响:

NSString *const centerX = @"center.x";
NSString *const centerY = @"center.y";

//#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

@implementation UIView (TLMotionEffect)

- (void)addCenterMotionEffectsXYWithOffset:(CGFloat)offset {

//    if(!IS_OS_7_OR_LATER) return;
    if(floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) return;

    UIInterpolatingMotionEffect *xAxis;
    UIInterpolatingMotionEffect *yAxis;

    xAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:centerX type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    xAxis.maximumRelativeValue = @(offset);
    xAxis.minimumRelativeValue = @(-offset);

    yAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:centerY type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    yAxis.minimumRelativeValue = @(-offset);
    yAxis.maximumRelativeValue = @(offset);

    UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
    group.motionEffects = @[xAxis, yAxis];

    [self addMotionEffect:group];
}

@end
NSString*const centerX=@“center.x”;
NSString*const centerY=@“center.y”;
//#定义为7或更高版本([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
@实现UIView(TLMotionEffect)
-(无效)addCenterMotionEffectsXYWithOffset:(CGFloat)偏移{
//如果(!IS_OS_7_或更高版本)返回;

if(floor(NSFoundationVersionNumber)@veducm的解决方案可以稍微短一点。如果分别添加x轴和y轴运动效果,则其x轴和y轴运动的UIMotionEffectGroup将过时

UIInterpolatingMotionEffect *motionEffect;
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
                                                               type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = @(-25);
motionEffect.maximumRelativeValue = @(25);
[bgView addMotionEffect:motionEffect];

motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
                                                               type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
motionEffect.minimumRelativeValue = @(-25);
motionEffect.maximumRelativeValue = @(25);
[bgView addMotionEffect:motionEffect];
快速翻译:

// Set vertical effect
let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -value
verticalMotionEffect.maximumRelativeValue = value

// Set vertical effect
let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis)
verticalMotionEffect.minimumRelativeValue = -value
verticalMotionEffect.maximumRelativeValue = value

let group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
self.motionEffect = group
self.addMotionEffect(group)

如果有人懒惰,请翻译成swift。如果你觉得有用,请投票@veducm

@IBOutlet var background : UIImageView!

func parallaxEffectOnBackground() {
    let relativeMotionValue = 50
    var verticalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y",
        type: .TiltAlongVerticalAxis)
    verticalMotionEffect.minimumRelativeValue = -relativeMotionValue
    verticalMotionEffect.maximumRelativeValue = relativeMotionValue

    var horizontalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x",
        type: .TiltAlongHorizontalAxis)
    horizontalMotionEffect.minimumRelativeValue = -relativeMotionValue
    horizontalMotionEffect.maximumRelativeValue = relativeMotionValue

    var group : UIMotionEffectGroup = UIMotionEffectGroup()
    group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]

    self.background.addMotionEffect(group)
}

这将帮助那些希望为tableView或collectionView实现视差的人。

  • 首先,为tableview创建单元格,并将图像视图放入其中

  • 将图像高度设置为略高于单元高度。如果单元高度=160,则将图像高度设置为200(以产生视差效果,您可以相应地更改它)

  • 将这两个变量放入viewController或扩展tableView委托的任何类中

  • 在同一类中添加以下代码
  • 其中seriestableview是我的UItableview

  • 现在让我们转到这个tableView的单元格并添加以下代码

  • 我们的后期图像是我的UIImageView
如果要对collectionView实现此功能,只需将tableView变量更改为collectionView变量


就是这样。我不确定这是否是最好的方法。但它对我有效。希望它也对你有效。如果有任何问题,请告诉我。真的很容易做到。为什么要引用复杂的代码。试试看。工作

在图像视图上查看图像视图的精确大小。 默认情况下,视图集0为alpha

//MARK: Scroll View Delegate methods
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

NSLog(@"X: %f Y: %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

CGFloat scrollY = _mainScrollView.contentOffset.y;
CGFloat height = _alphaView.frame.size.height;

CGFloat alphaMonitor = scrollY/height;

_alphaView.alpha = alphaMonitor;
}

查看此库:对于swift,请尝试:NGAParallaxMotion适用于iOS 7+,不适用于较旧的iOS版本。它为UIView提供了一个类别,允许您设置.parallaxIntensity值,使用一行代码设置UIMotionEffect参数。谢谢!我已更新了答案,以包括支持的版本和要求。实际情况LLY,你想把运动效果添加到背景视图中,而不是前视图。但是,除此之外,这个代码太好了!谢谢。有人知道如何检查用户是否已经禁用了效果?@ GSTUUP实际上,在主屏幕上,前景和背景都在移动。但是如果你只选择一个层来应用PAR所有的效果,我同意背景可能是最好的。就像任何人使用这个缩短版本的提示一样,第一个运动效果需要有
类型
UIInterpolingMotionEffectTypeTiltalongHorizontalAxis
而不是
UIInterpolingMotionEffectTypeTiltalongVerticalAxis
,因为它在代码中铺设out@BooHoo将它们作为一个组添加是过时的?你在哪里看到的?截至本文撰写之时,Apple文档没有任何关于它过时的信息。对它们进行分组的全部目的是提高性能。组中的所有效果都渲染一次。当你单独应用它们时,它们必须单独渲染。请放心o如果我错了,请纠正我(并向我指出苹果的相关文档。)@Rob根据您的建议更新了答案虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能会无效。在此处很难集成整个github项目您不必带上全部内容项目中,只有我通过编辑答案演示的关键部分。这样,代码才能在Github崩溃、你拉回购协议或天崩地裂的情况下保持不变。谢谢@Lucas!我已经更新了我的答案,将swift示例也包括在内。它基于此示例。请随时查看并进行任何必要的更改。
@IBOutlet var background : UIImageView!

func parallaxEffectOnBackground() {
    let relativeMotionValue = 50
    var verticalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y",
        type: .TiltAlongVerticalAxis)
    verticalMotionEffect.minimumRelativeValue = -relativeMotionValue
    verticalMotionEffect.maximumRelativeValue = relativeMotionValue

    var horizontalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x",
        type: .TiltAlongHorizontalAxis)
    horizontalMotionEffect.minimumRelativeValue = -relativeMotionValue
    horizontalMotionEffect.maximumRelativeValue = relativeMotionValue

    var group : UIMotionEffectGroup = UIMotionEffectGroup()
    group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]

    self.background.addMotionEffect(group)
}
let imageHeight:CGFloat = 150.0
let OffsetSpeed: CGFloat = 25.0
 func scrollViewDidScroll(scrollView: UIScrollView) {
    //  print("inside scroll")

    if let visibleCells = seriesTabelView.visibleCells as? [SeriesTableViewCell] {
        for parallaxCell in visibleCells {
            var yOffset = ((seriesTabelView.contentOffset.y - parallaxCell.frame.origin.y) / imageHeight) * OffsetSpeedTwo
            parallaxCell.offset(CGPointMake(0.0, yOffset))
        }
    }
}
func offset(offset: CGPoint) {
        posterImage.frame = CGRectOffset(self.posterImage.bounds, offset.x, offset.y)
    }
//MARK: Scroll View Delegate methods
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

NSLog(@"X: %f Y: %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

CGFloat scrollY = _mainScrollView.contentOffset.y;
CGFloat height = _alphaView.frame.size.height;

CGFloat alphaMonitor = scrollY/height;

_alphaView.alpha = alphaMonitor;
}