图案图像如何实现视差效果?
How to achieve parallax effect with pattern image?
我一直在尝试实现图像视图的视差效果。视差效果的代码很简单,仅当我将 UIImage
添加到 UIImageView
时它才有效。我有一个图案图像,需要在两侧平铺。
所以我不得不使用UIColor
的patternWithImage
方法。当我进行视差工作时,但是当我倾斜设备时,我可以在边缘看到 self.view
的背景颜色。
这是一个代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.backgroundImageView.contentMode = UIViewContentModeCenter;
self.backgroundImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"connect"]];
[self addParallaxEffectToView:self.backgroundImageView];
}
- (void)addParallaxEffectToView:(UIView *)view
{
// Set vertical effect
UIInterpolatingMotionEffect *verticalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-50);
verticalMotionEffect.maximumRelativeValue = @(50);
// Set horizontal effect
UIInterpolatingMotionEffect *horizontalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-50);
horizontalMotionEffect.maximumRelativeValue = @(50);
// Create group to combine both
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];
// Add both effects to your view
[view addMotionEffect:group];
}
代码可以在 Swift 或 Objective-C 中。任何帮助,将不胜感激。谢谢
编辑:
根据@Clafou 的建议,当我尝试推送和弹出动画时,我看起来很奇怪。
我猜你的 backgroundImageView
需要延伸到容器的边缘之外,因为你的效果会移动它。
作为快速测试,如果您不使用 AutoLayout,您可以将 self.backgroundImageView.frame = CGRectInset(self.backgroundImageView.frame, -50, -50);
添加到 viewDidLoad 的末尾。
如果您使用 AutoLayout,请更改约束以使 backgroundImageView
超出其容器的框架。
我一直在尝试实现图像视图的视差效果。视差效果的代码很简单,仅当我将 UIImage
添加到 UIImageView
时它才有效。我有一个图案图像,需要在两侧平铺。
所以我不得不使用UIColor
的patternWithImage
方法。当我进行视差工作时,但是当我倾斜设备时,我可以在边缘看到 self.view
的背景颜色。
这是一个代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.backgroundImageView.contentMode = UIViewContentModeCenter;
self.backgroundImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"connect"]];
[self addParallaxEffectToView:self.backgroundImageView];
}
- (void)addParallaxEffectToView:(UIView *)view
{
// Set vertical effect
UIInterpolatingMotionEffect *verticalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-50);
verticalMotionEffect.maximumRelativeValue = @(50);
// Set horizontal effect
UIInterpolatingMotionEffect *horizontalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-50);
horizontalMotionEffect.maximumRelativeValue = @(50);
// Create group to combine both
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];
// Add both effects to your view
[view addMotionEffect:group];
}
代码可以在 Swift 或 Objective-C 中。任何帮助,将不胜感激。谢谢
编辑: 根据@Clafou 的建议,当我尝试推送和弹出动画时,我看起来很奇怪。
我猜你的 backgroundImageView
需要延伸到容器的边缘之外,因为你的效果会移动它。
作为快速测试,如果您不使用 AutoLayout,您可以将 self.backgroundImageView.frame = CGRectInset(self.backgroundImageView.frame, -50, -50);
添加到 viewDidLoad 的末尾。
如果您使用 AutoLayout,请更改约束以使 backgroundImageView
超出其容器的框架。