如何在IOS中应用部分淡入?
我有2张图片。 一个在另一个的后面。 我想淡出从左上angular开始的顶部图像。 我怎样才能做到这一点?
我应该使用渐变面具吗?
这是一种技巧,可以淡化CALayer中的任何内容,从左上angular到右下angular,揭示CALayer下面的任何内容。
比方说,我们将淡出名为self.fadeView
的UIImageView
self.fadeView
。
我们将创build一个CAGradientLayer
并将其用作self.fadeView.layer.mask
。 渐变将使用四个站点:0,0,1,1,从alpha = 0(透明)到alpha = 1(不透明)。是,两个零,然后是两个。 当我们希望fadeView
是不透明的,我们将停止位置设置为-1,-5,0,1。这样,两个alpha = 0停止完全在层边界之外。 当我们希望fadeView
是透明的,我们将停止位置设置为fadeView
。这样,两个alpha = 1停止完全在层边界之外。 CAGradientLayer
会自动为其停止位置设置animation,创build淡入淡出效果。
代码如下:
#import "ViewController.h" @implementation ViewController @synthesize fadeView = _fadeView; static NSArray *locations(float a, float b, float c, float d) { return [NSArray arrayWithObjects: [NSNumber numberWithFloat:a], [NSNumber numberWithFloat:b], [NSNumber numberWithFloat:c], [NSNumber numberWithFloat:d], nil]; } // In my test project, I put a swipe gesture recognizer on fadeView in my XIB // with direction = Up and connected it to this action. - (IBAction)fadeIn { [CATransaction begin]; [CATransaction setValue:[NSNumber numberWithDouble:2.0] forKey:kCATransactionAnimationDuration]; ((CAGradientLayer *)self.fadeView.layer.mask).locations = locations(-1, -.5, 0, 1); [CATransaction commit]; } // In my test project, I put a swipe gesture recognizer on fadeView in my XIB // with direction = Down and connected it to this action. - (IBAction)fadeOut { [CATransaction begin]; [CATransaction setValue:[NSNumber numberWithDouble:2.0] forKey:kCATransactionAnimationDuration]; ((CAGradientLayer *)self.fadeView.layer.mask).locations = locations(0, 1, 1.5, 2); [CATransaction commit]; } - (void)viewDidLoad { [super viewDidLoad]; CAGradientLayer *mask = [CAGradientLayer layer]; mask.frame = self.fadeView.bounds; mask.colors = [NSArray arrayWithObjects: (__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor, nil]; mask.startPoint = CGPointZero; // top left corner mask.endPoint = CGPointMake(1, 1); // bottom right corner self.fadeView.layer.mask = mask; [self fadeIn]; // initialize mask.locations } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end
使用自己的actionForKey
animation定义您自己的CALayer
子类的关键contents
。
@interface CustomLayer: CALayer @end @implementation CustomLayer + (id<CAAction>)actionForKey:(NSString*)event { if ([event isEqualToString:@"contents"]) { CATransition* basicAnimation = [CATransition animation]; basicAnimation.type = kCATransitionMoveIn; basicAnimation.subtype = kCATransitionFromLeft; return basicAnimation; } else { return nil; } } @end
这样做,无论何时设置CustomLayer
对象的contents
属性,都会显示转换:
CustomLayer* layer = [CustomLayer layer]; layer.contents = <FIRST_CGIMAGEREF_HERE>; ... layer.contents = <SECOND_CGIMAGEREF_HERE>;
像往常一样,您可以将属性分配包装在一个事务中:
[CATransaction begin]; .... [CATransaction end];
如果您希望对转换的持续时间有更多的控制权,但是在任何情况下都将应用转换。
编辑:
对于你想要的那种转换,你可以在GitHub上看一下这个例子 。 寻找ShutterTransition。 这不是你正在寻找的东西,但它可能导致你沿着正确的道路。
- 你如何创build一个类似于iphone删除animation的摆动animation
- CoreAnimation性能分析 – 带有CAShapeLayer的CAReplicatorLayer
- 如何使UIScrollView在animation过程中发送scrollViewDidScroll消息
- CAScrollLayer中的自卷动文本
- iPhone“滑动解锁”animation
- 自定义视图与CALayer的有线效果,当animation边界改变
- 在animation之前改进HTML5 Canvas逐帧JPGanimation来完全caching
- 使用Core Animation完成自定义animation – 性能
- 当有很多使用阴影的UILabels运行的UIViewanimation时,设备会冻结,然后按Homebutton