iOS view渐变透明层遮盖

干就完了

项目中用到地图设置渐变色,查找资料找到两种方法:

一种设置颜色:为颜色设置渐变效果:

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    CAGradientLayer *gradient = [CAGradientLayer layer];

    //设置开始和结束位置(设置渐变的方向)

    gradient.startPoint = CGPointMake(0, 0);

    gradient.endPoint = CGPointMake(1, 0);

    gradient.frame =CGRectMake(0,0,40,40);

    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColorwhiteColor].CGColor,nil];

    [view.layer insertSublayer:gradient atIndex:0];

    [self.view addSubview:view];

    


一种设置透明度:为透明度设置渐变效果

    [self.viewaddSubview:self.gradientV];

    self.gradientV.frame =CGRectMake(ORIGINX(self.chatMessageVC.view),ORIGINY(self.chatMessageVC.view),WIDTH(self.chatMessageVC.view), gradient_height);

    CAGradientLayer *_gradLayer = [CAGradientLayerlayer];

    NSArray *colors = [NSArrayarrayWithObjects:

                      //                       (id)[[UIColor colorWithWhite:1 alpha:1] CGColor],

                      (id)[[UIColor colorWithWhite:1 alpha:0.8]CGColor],

//                      (id)[[UIColor colorWithWhite:1 alpha:0.6] CGColor],

                      (id)[[UIColor colorWithWhite:1 alpha:0.4]CGColor],

                      (id)[[UIColor colorWithWhite:1 alpha:0.2]CGColor],

                      (id)[[UIColor colorWithWhite:1 alpha:0]CGColor],

                      nil];

    [_gradLayersetColors:colors];

    //渐变起止点,point表示向量

   [_gradLayer setStartPoint:CGPointMake(0.0f,0.0f)];

    [_gradLayersetEndPoint:CGPointMake(0.0f,1.0f)];

    [_gradLayersetFrame:CGRectMake(0,0, WIDTH(self.gradientV),HEIGHT(self.gradientV))];

    [self.gradientV.layerinsertSublayer:_gradLayer atIndex:0];


view曾加阴影

        self.headerBtn.layer.cornerRadius =46/2;

        self.headerBtn.layer.shadowColor =setColorWithAlpha(@"#6bc5fc",0.5).CGColor;

        self.headerBtn.layer.shadowOpacity =0.4;//设置阴影的透明度

        self.headerBtn.layer.shadowOffset =CGSizeMake(1, 10);//设置阴影的偏移量

        self.headerBtn.layer.shadowRadius =3;//设置阴影的圆角




你可能感兴趣的:(iOS)