Here is the code i Use to apply CAGradient layer On UIIimageView .It works on one Edge but i am not able to apply on other edges. I use two CAGradient layers To apply on left and Top of ImageView To Fade it.But It Only applied one side of UIImageview.
This is my Code.
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];
}
Try this.. It may help you.