iOS 将图像向上滑动到图像上,显示下面的图像。 (A-la jQuery.slide())

标签 ios animation uiimage slide

我正在尝试做以下事情 -

我有同一张图片的两个版本,一个是彩色的,一个是黑白的。我希望 B&W 能够“向上滑动”以显示其下方的那个。

我尝试设置高度和框架,但无法按我想要的方式运行。

例如,这将是两个 UIImages 的组合,其中一个显示另一个,但不移动:

enter image description here

有什么想法吗? :)

最佳答案

好的,这是在灰色 View 上使用 mask 的另一种方法。我实际上用 blueView 和 greyView 对此进行了测试,其中灰色覆盖了蓝色。在灰色层上放置一个 mask 层,使灰色层可见。现在使蒙版远离灰色层,使灰色层消失而不移动它,蓝色显示灰色消失的地方。

如果您想对此进行试验,请在 XCode 中创建一个具有单一 View 的空项目,并将此代码放入 viewDidLoad 中。我就是这样测试的。

self.view.backgroundColor = [UIColor whiteColor];

UIView* blueView = [[[UIView alloc] init] autorelease];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(50,50,100,100);

UIView* grayView = [[[UIView alloc] init] autorelease];
grayView.backgroundColor = [UIColor grayColor];
grayView.frame = CGRectMake(50,50,100,100);

[self.view addSubview:blueView];
[self.view addSubview:grayView];    // gray covers the blue

// Create a mask to allow the grey view to be visible and cover up the blue view
CALayer* mask = [CALayer layer];
mask.contentsScale   = grayView.layer.contentsScale;  // handle retina scaling
mask.frame           = grayView.layer.bounds;
mask.backgroundColor = [UIColor blackColor].CGColor;
grayView.layer.mask = mask;

// Animate the position of the mask off the grey view, as the grey becomes unmasked,
// that part of the grey "dissappears" and is no longer covering the blue underneath
CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:@"position"];
a.duration  = 4;
a.fromValue = [NSValue valueWithCGPoint:mask.position];
CGPoint newPosition = mask.position;
newPosition.y += mask.bounds.size.height;
a.toValue   = [NSValue valueWithCGPoint:newPosition];
a.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[mask addAnimation:a forKey:@"colorize"];   // animate presentation

mask.position = newPosition;        // update actual position

不要忘记顶部带有#imports 的这一行:

#import <QuartzCore/QuartzCore.h>

关于iOS 将图像向上滑动到图像上,显示下面的图像。 (A-la jQuery.slide()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306886/

相关文章:

ios - 动态中的 Swift 自定义滑动 TableView 单元格问题

iOS Safari 显示已安装的应用程序。这是如何运作的?元标签?

ios - 在 UITextView Objective-C 中的文本末尾添加更多按钮?

objective-c - 同时动画旋转/缩放/平移 UIImage

ios - 使用 Swift 保存带有圆角和边框的 UIImage

ios - 调用 [UIImage imageWithData :xxx] multiple times

ios - 尝试添加 UIImage 作为 Storyboard中的背景(不重叠我的其他 ViewController 元素)

iOS 应用提交 : missing 64-bit support

jquery - jQuery 中的按钮转换

javascript - React Spring - 渲染之间的动画元素