这是我用来在 UIIimageView 上应用 CAGradient 层的代码。它在一个边缘上工作,但我无法在其他边缘上应用。我使用两个 CAGradient 层来应用在 ImageView 的左侧和顶部来淡化它。但它只应用了 UIImageview 的一侧。
这是我的代码。
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,160,300)];
imgView.image=[UIImage imageNamed:@"1.jpg"];
[self.view addSubview:imgView];
UIImageView *imgView1=[[UIImageView alloc] initWithFrame:CGRectMake(100,0,160,300)];
imgView1.image=[UIImage imageNamed:@"2.jpg"];
[self.view addSubview:imgView1];
CAGradientLayer *l = [CAGradientLayer layer];
l.frame =imgView1.bounds;
l.colors =[NSArray arrayWithObjects:
( id)[UIColor clearColor].CGColor,
( id)[UIColor clearColor].CGColor,
( id)[UIColor whiteColor].CGColor,
( id)[UIColor whiteColor].CGColor,
nil];
l.startPoint = CGPointZero; // top left corner
l.endPoint = CGPointMake(50,1);
imgView1.layer.mask =l;
[self fadeIn:imgView1];
CAGradientLayer *l2 = [CAGradientLayer layer];
l2.frame =imgView1.bounds;
l2.colors =[NSArray arrayWithObjects:
( id)[UIColor clearColor].CGColor,
( id)[UIColor clearColor].CGColor,
( id)[UIColor whiteColor].CGColor,
( id)[UIColor whiteColor].CGColor,
nil];
l2.startPoint = CGPointZero; // top left corner
l2.endPoint = CGPointMake(1,50);
imgView1.layer.mask =l2;
[self fadeIn:imgView1];
static NSArray *locations(float a, float b, float c, float d, float e, float f, float g)
{
return [NSArray arrayWithObjects:
[NSNumber numberWithFloat:a],
[NSNumber numberWithFloat:b],
[NSNumber numberWithFloat:c],
[NSNumber numberWithFloat:d],
[NSNumber numberWithFloat:e],
[NSNumber numberWithFloat:f],
[NSNumber numberWithFloat:g],
nil];
}
- (IBAction)fadeIn:(UIImageView*)img
{
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithDouble:1.0] forKey:kCATransactionAnimationDuration];
((CAGradientLayer *)img.layer.mask).locations = locations(-1.5,-1.0,-0.5,0,0.5,1,1.5);
[CATransaction commit];
}