[英]xcode/ios7: parallax effect with image clipped inside view
我正在嘗試實現iOS7視差效果。 為此,我使用標准的UIInterpolatingMotionEffect類。
這是我要實現的目標:
幾乎網上的每個教程都只是在移動整個視圖(通過設置center.x),但是我不知道如何僅移動內容(並同時裁剪內容)。 我嘗試了一些東西,但顯然無法正常工作:
在viewDidLoad內部,我接下來要做的是:
- (void)viewDidLoad
{
[super viewDidLoad];
_myView.layer.contents = (id) [UIImage imageNamed:@"dog"].CGImage;
_myView.layer.contentsGravity = kCAGravityCenter;
_myView.layer.masksToBounds = YES;
UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.position.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-80);
horizontalMotionEffect.maximumRelativeValue = @(80);
[_myView addMotionEffect:horizontalMotionEffect];
}
CALayer沒有addMotionEffect,所以我正在訪問視圖。
也許我的方法從一開始就不好,所以如果您有其他解決方案-會有所幫助。
感謝您的任何幫助。
我認為這里更好的方法可能只是使用2個單獨的視圖。
UIView
大小與您想要的大小相同,並具有拐角半徑和clipsToBounds
設置為YES
。 UIImageView
,添加為UIView
的子視圖。 然后,僅將運動效果應用於圖像視圖。 這將使包含的視圖保持靜態,並實現所需的鎖孔效果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.