UIIimageView に CAGradient レイヤーを適用するために使用するコードは次のとおりです。これは 1 つのエッジで動作しますが、他のエッジには適用できません。私は 2 つの 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];
}