1. 程式人生 > >ios開發UIButton背景色漸變

ios開發UIButton背景色漸變

UIButton *thirdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    thirdBtn.frame = CGRectMake(10, 50, 200, 44);

    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, 200, 44);
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.locations = @[@(0.5),@(1.0)];//漸變點
    [gradientLayer setColors:@[(id)[[UIColor redColor] CGColor],(id)[RGB(100, 100, 100) CGColor]]];//漸變陣列
    [thirdBtn.layer addSublayer:gradientLayer];
    
    [thirdBtn setTitle:@"程式碼建立的按鈕,使用layer" forState:UIControlStateNormal];
    [self.view addSubview:thirdBtn]