UIImage上的iOSanimation蒙版

我正在使用Swift 1.2,我的目标是在静态UIImage上为图像蒙版设置animation。 我所实现的是一个掩盖我最初在Objective-C中find的图像的快速版本。

func maskImage(image: UIImage, mask: UIImage) -> UIImage! { let maskRef = mask.CGImage; let mask = CGImageMaskCreate( CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), nil, false); let masked = CGImageCreateWithMask(image.CGImage, mask); let retImage = UIImage(CGImage: masked); return retImage; } 

这很好用! 但是,把它付诸实施是我的挑战。

有没有办法迭代应用与不同的水平偏移掩模或更好的方式来完全解决这个问题 – 也许用CALayer实现?

谢谢你的帮助!

编辑:根据什么张贴作为答案,我补充说:

  let image = UIImage(named: "clouds"); let imageView = UIImageView(image: image); let layer = CALayer(); layer.contents = UIImage(named: "alpha-mask")?.CGImage; layer.bounds = CGRectMake(0, 0, image.size.width, image.size.height); // For other folks learning, this did not work //let animation = CABasicAnimation(keyPath: "bounds.origin.x"); // This does work let animation = CABasicAnimation(keyPath: "position.x"); animation.duration = 2; animation.delegate = self; animation.fillMode = kCAFillModeForwards; animation.repeatCount = 0; animation.fromValue = 0.0; animation.toValue = image.size.width; animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear); animation.removedOnCompletion = false; layer.addAnimation(animation, forKey: "transform"); imageView.layer.mask = layer; self.addSubview(imageView); 

我能够正确地看到alpha蒙版,但animation不起作用。 有任何想法吗?

编辑:我修改了上面的代码,它的工作原理! 我需要做keyPath position.x。 往上看

你确实想要使用一个CALayer – 或者说,一个CAShapeLayer。

您可以创build一个CAShapeLayer并将其作为另一个图层上的遮罩进行安装。

您可以创build一个CAAnimation,将animation更改为形状图层的path,也可以为该图层的strokeStart和/或strokeEnd属性设置animation效果。

如果为path设置了animation,则要遵循的一条规则是确保开始和结束path具有相同的控制点数量和types。 否则animation是“未定义的”,结果可能很奇怪。

我有一个发展博客文章,概述了它是如何完成的:

http://wareto.com/using-core-animation-groups-to-create-animation-sequences-2

这主要是关于使用CAAnimationGroups,但它也包括一个工作示例,animation更改CAShapeLayer作为图像视图图层的蒙版。

下面是它创build的蒙版animation的GIF – 显示和隐藏图像视图的“时钟擦除”:

在这里输入图像说明

不幸的是它是用Objective-C编写的,但是核心animation调用在Swift中几乎是一样的。 让我知道,如果你有任何问题,找出如何适应它。

animation代码的肉是这样的:

 - (IBAction)doMaskAnimation:(id)sender; { waretoLogoLarge.hidden = FALSE;//Show the image view //Create a shape layer that we will use as a mask for the waretoLogoLarge image view CAShapeLayer *maskLayer = [CAShapeLayer layer]; CGFloat maskHeight = waretoLogoLarge.layer.bounds.size.height; CGFloat maskWidth = waretoLogoLarge.layer.bounds.size.width; CGPoint centerPoint; centerPoint = CGPointMake( maskWidth/2, maskHeight/2); //Make the radius of our arc large enough to reach into the corners of the image view. CGFloat radius = sqrtf(maskWidth * maskWidth + maskHeight * maskHeight)/2; //Don't fill the path, but stroke it in black. maskLayer.fillColor = [[UIColor clearColor] CGColor]; maskLayer.strokeColor = [[UIColor blackColor] CGColor]; maskLayer.lineWidth = radius; //Make the line thick enough to completely fill the circle we're drawing CGMutablePathRef arcPath = CGPathCreateMutable(); //Move to the starting point of the arc so there is no initial line connecting to the arc CGPathMoveToPoint(arcPath, nil, centerPoint.x, centerPoint.y-radius/2); //Create an arc at 1/2 our circle radius, with a line thickess of the full circle radius CGPathAddArc(arcPath, nil, centerPoint.x, centerPoint.y, radius/2, 3*M_PI/2, -M_PI/2, YES); maskLayer.path = arcPath; //Start with an empty mask path (draw 0% of the arc) maskLayer.strokeEnd = 0.0; CFRelease(arcPath); //Install the mask layer into out image view's layer. waretoLogoLarge.layer.mask = maskLayer; //Set our mask layer's frame to the parent layer's bounds. waretoLogoLarge.layer.mask.frame = waretoLogoLarge.layer.bounds; //Create an animation that increases the stroke length to 1, then reverses it back to zero. CABasicAnimation *swipe = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; swipe.duration = 2; swipe.delegate = self; [swipe setValue: theBlock forKey: kAnimationCompletionBlock]; swipe.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; swipe.fillMode = kCAFillModeForwards; swipe.removedOnCompletion = NO; swipe.autoreverses = YES; swipe.toValue = [NSNumber numberWithFloat: 1.0]; [maskLayer addAnimation: swipe forKey: @"strokeEnd"]; } 

我有另一个在Swift中的博客条目,展示了如何使用CAShapeLayer创build和animation饼图。 该项目animation的形状,而不是一个面具,但唯一真正的区别是,你是否安装形状层作为一个常规的内容层,或作为一个掩膜在另一层,如图像视图的背景层。

您可以在此链接查看该项目:

http://wareto.com/swift-piecharts