如何在IOS中应用部分淡入淡出?
我有两张照片。一个在另一个的后面。我想从左角开始淡出顶部图像。我该怎么做如何在IOS中应用部分淡入淡出?,ios,core-animation,fadeout,Ios,Core Animation,Fadeout,我有两张照片。一个在另一个的后面。我想从左角开始淡出顶部图像。我该怎么做 我应该使用渐变蒙版吗?为关键帧内容定义自己的CALayer子类,并使用自己的actionForKey动画 @interface CustomLayer: CALayer @end @implementation CustomLayer + (id<CAAction>)actionForKey:(NSString*)event { if ([event isEqualToString:@"content
我应该使用渐变蒙版吗?为关键帧
内容定义自己的CALayer
子类,并使用自己的actionForKey
动画
@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
与往常一样,您可以在事务中包装属性分配:
[CATransaction begin];
....
[CATransaction end];
如果您希望对转换的持续时间进行更多控制,但无论如何都会应用转换
编辑:
对于您想要的过渡类型,您可以看一看。寻找转换。这并不完全是你想要的,但它可以引导你走上正确的道路。这里有一种技术,它可以从左上到右下淡出CALayer中的任何内容,显示CALayer下面的任何内容
假设我们将淡出名为self.fadeView
的UIImageView
层
我们将创建一个并将其用作self.fadeView.layer.mask
。渐变将使用四个停止点从alpha=0(透明)变为alpha=1(不透明):0、0、1、1。是的,两个零,然后两个一。当我们希望fadeView
不透明时,我们将停止位置设置为-1、.5、0、1。这样,两个alpha=0停止点就完全超出了层边界。当我们希望fadeView透明时,我们会将停止位置设置为0、1、1.5、2。这样,两个alpha=1停止点就完全超出了层边界。CAGradientLayer
将自动为其停止位置的更改设置动画,创建交叉淡入淡出效果
代码如下:
#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
是否使用两个UIImageView?两个加莱耶?对我来说没关系。我想你的建议是为了过渡。我需要的是类似“谢谢”的东西,但我无法让代码正常工作。你有一个样本项目吗?正是我需要的!非常感谢你!!先生,我还想将动画与iPad的滚动或俯仰运动联系起来。我正在从传感器获取角度。所以,当我滚动iPad时,交叉淡入应该像在中一样发生。有什么想法吗?你可以通过改变
mask.startPoint
和mask.endPoint
来改变淡入淡出的方向。是的,但例如,如果我将iPad滚动1度,渐变应该改变1度。我能做这个吗?
#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