2013-11-04 3 views
2

사용자 지정 UIButton 그라데이션 배경을 만들고 싶습니다. 요구 사항에 대한 사진을 첨부했습니다. 내가 예상대로
enter image description here
enter image description hereUIButton에 대한 사용자 지정 그라디언트 배경을 설정하는 방법?

는하지만 내 그라데이션 배경은 없습니다.

enter image description here

내 질문은 어떻게 그라데이션에 위치 또는 위치를 설정하는 것입니다? (위에서 아래로 중심에서부터 중심까지).

여기 내 코드입니다 :

- (void)setBlueShiningLayer { 
    CALayer *buttonLayer = self.layer; 
    [buttonLayer setMasksToBounds:YES]; 
    [buttonLayer setCornerRadius:5.0]; 
    [buttonLayer setBorderWidth:1.0f]; 
    [buttonLayer setBorderColor:[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f].CGColor]; 

    CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 
    [gradientLayer setBounds:self.bounds]; 
    NSArray *colors = [NSArray arrayWithObjects: 
      (id) [UIColor colorWithRed:129.0f/255.0f green:151.0f/255.0f blue:179.0f/255.0f alpha:0.8f].CGColor, // top 
      (id) [UIColor colorWithRed:111.0f/245.0f green:133.0f/255.0f blue:162.0f/255.0f alpha:0.4f].CGColor, // center 
      (id) [UIColor colorWithRed:95.0f/245.0f green:118.0f/255.0f blue:151.0f/255.0f alpha:0.4f].CGColor, // center 
      (id) [UIColor colorWithRed:75.0f/245.0f green:99.0f/255.0f blue:133.0f/255.0f alpha:0.8f].CGColor, // bottom 
      nil]; 

    [gradientLayer setPosition:CGPointMake([self bounds].size.width/2, [self bounds].size.height/2)]; 
    [gradientLayer setColors:colors]; 
    [buttonLayer insertSublayer:gradientLayer atIndex:0]; 

    [self setTitleColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f] forState:UIControlStateNormal]; 
    [self setTintColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f]]; 
} 
+0

내 답변이 문제를 정리 했습니까? – Fogmeister

답변

2

이 균일하게 색상을 밖으로 간격됩니다 그라디언트 레이어처럼 보인다. 그것이 당신이 얻는 효과를 얻고있는 이유입니다.

이 문제를 해결하려면 locations 속성을 CAGradientLayer으로 사용할 수 있습니다. 이처럼

...

gradientLayer.locations = @[@0, @0.5, @0.5, @1.0]; 

TBH하는 UIButton이 작업을 수행하는 가장 쉬운 방법은 이미지를 사용하는 것입니다.

원하는 그라디언트와 테두리로 버튼 이미지를 만든 다음 배경 이미지를 추가하십시오.

다른 크기로 사용해야하는 경우 크기를 조정할 수있는 이미지를 만들 수 있습니다.

+0

고마워 지금은 작동하지만 마지막 위치도 0으로 설정해야합니다. 그리고 나는 이미지를 사용하는 것에 동의하지만, 디자이너는 그림 대신 RGB를 보내줍니다. :) – kameny

관련 문제