我想創建一個自定義的UIButton漸變背景。我附上了一些關於要求的圖片。
如何爲UIButton設置自定義漸變背景?
但我的漸變背景是不是如我所料。
我的問題是如何設置漸變的位置或位置? (從頂部到中心,然後從中心到底部)。
這裏是我的代碼:
- (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]];
}
我的回答是否對您的問題進行排序? – Fogmeister