2012年6月5日 星期二

UIView 視覺效果:圓角、陰影、邊框、漸層光澤


先建立一個 UIView 物件:
1
2
3
4
5
6
// 記得在 header 檔裡引入 QuartzCore
#import <QuartzCore/QuartzCore.h>

UIView *sampleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
sampleView.backgroundColor = [UIColor whiteColor]; // 把背景設成白色
// sampleView.backgroundColor = [UIColor clearColor]; // 透明背景

圓角

1
2
sampleView.layer.cornerRadius = 2.5; // 圓角的弧度
sampleView.layer.masksToBounds = YES;

陰影

1
2
3
4
sampleView.layer.shadowColor = [[UIColor blackColor] CGColor];
sampleView.layer.shadowOffset = CGSizeMake(3.0f, 3.0f); // [水平偏移, 垂直偏移]
sampleView.layer.shadowOpacity = 0.5f; // 0.0 ~ 1.0 的值
sampleView.layer.shadowRadius = 10.0f; // 陰影發散的程度

邊框

1
2
sampleView.layer.borderWidth = 1;
sampleView.layer.borderColor = [[UIColor blackColor] CGColor];

漸層光澤

1
2
3
4
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = sampleView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor grayColor] CGColor], nil]; // 由上到下的漸層顏色
[sampleView.layer insertSublayer:gradient atIndex:0];
參考網站:http://gibuloto.com/blog/uiview/

沒有留言:

張貼留言