在UIImageView上应用CAGradient图层在上边缘和左边缘

时间:2014-01-03 11:03:03

标签: ios iphone objective-c xcode cagradientlayer

以下是我用于在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];
    }

1 个答案:

答案 0 :(得分:0)

试试这个..它可能对你有帮助。

CAGradientLayer *l = [CAGradientLayer layer];
l.doubleSided = YES;