[self conrnerRadiusTest];
//shadowPath
[self shadowPathTest];
//mask
[self maskTest];
//拉伸過濾
[self filterTest];
//alpha
[self alphaTest];
圓角conrnerRadius:默認情況下衣洁,這個曲率值只影響背景顏色而不影響背景圖片或是子圖層。不過攒读,如果把masksToBounds設置成YES的話绸贡,圖層里面的所有東西都會被截取。
- (void)conrnerRadiusTest {
UIView *layerView1 = [[UIView alloc] init];
layerView1.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
layerView1.backgroundColor = [UIColor redColor];
UIView *layerView2 = [[UIView alloc] init];
layerView2.frame = CGRectMake(20.0f, 20.0f, 100.0f, 100.0f);
layerView2.backgroundColor = [UIColor blueColor];
[layerView2 addSubview:layerView1];
UIView *shadowView = [[UIView alloc] init];
shadowView.frame = CGRectMake(20.0f, 20.0f, 100.0f, 100.0f);
shadowView.backgroundColor = [UIColor blueColor];
[self.view addSubview:shadowView];
[self.view addSubview:layerView2];
//set the corner radius on our layers
layerView1.layer.cornerRadius = 20.0f;
layerView2.layer.cornerRadius = 10.0f;
shadowView.layer.cornerRadius = 10.0f;
//add a border to your layers
layerView1.layer.borderWidth = 2.0f;
layerView2.layer.borderWidth = 2.0f;
//add a shadow to layerView1
layerView2.layer.shadowOpacity = 0.5f;
layerView2.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
layerView2.layer.shadowRadius = 5.0f;
//add same shadow to shadowView (not layerView2)
shadowView.layer.shadowOpacity = 0.5f;
shadowView.layer.shadowOffset = CGSizeMake(-10.0f, 10.0f);
shadowView.layer.shadowRadius = 5.0f;
//enable clipping on the second layer
layerView2.layer.masksToBounds = YES;
}
圖層邊框
//borderWidth:以點為單位的定義邊框粗細的浮點數(shù)凛辣,默認為0.
//borderColor:定義了邊框的顏色抱既,默認為黑色。 CGColorRef
陰影
//shadowOpacity:是一個必須在0.0(不可見)和1.0(完全不透明)之間的浮點數(shù) 默認值0
//CALayer的另外三個屬性:shadowColor(CGColorRef)扁誓,shadowOffset(CGSize)默認值是 {0, -3}和shadowRadius屬性控制著陰影的模糊度防泵。
陰影裁剪
//圖層的陰影繼承自內(nèi)容的外形,而不是根據(jù)邊界和角半徑來確定
//一個只畫陰影的空的外圖層蝗敢,和一個用masksToBounds裁剪內(nèi)容的內(nèi)圖層
shadowPath屬性
//shadowPath是一個CGPathRef類型(一個指向CGPath的指針)捷泞。CGPath是一個Core Graphics對象,用來指定任意的一個矢量圖形寿谴。我們可以通過這個屬性單獨于圖層形狀之外指定陰影的形狀锁右。
- (void)shadowPathTest {
UIView *layerView1 = [[UIView alloc] init];
layerView1.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
layerView1.backgroundColor = [UIColor redColor];
UIView *layerView2 = [[UIView alloc] init];
layerView2.frame = CGRectMake(200.0f, 200.0f, 100.0f, 100.0f);
layerView2.backgroundColor = [UIColor blueColor];
[self.view addSubview:layerView1];
[self.view addSubview:layerView2];
//enable layer shadows
layerView1.layer.shadowOpacity = 0.5f;
layerView2.layer.shadowOpacity = 0.5f;
//create a squre shadow
CGMutablePathRef squarePath = CGPathCreateMutable();
CGPathAddRect(squarePath, NULL, layerView1.bounds);
layerView1.layer.shadowPath = squarePath;
CGPathRelease(squarePath);
//create a cirular shadow
CGMutablePathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect(circlePath, NULL, layerView2.bounds);
layerView2.layer.shadowPath = circlePath;
CGPathRelease(circlePath);
}
圖層蒙板
//mask:CALayer類型 mask圖層定義了父圖層的部分可見區(qū)域
- (void)maskTest {
UIImageView *mealView = [[UIImageView alloc] init];
mealView.frame = CGRectMake(50.0f, 50.0f, 200.0f, 200.0f);
mealView.image = [UIImage imageNamed:@"Meal"];
[self.view addSubview:mealView];
//create mask layer
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = CGRectMake(10.0f, 10.0f, 60.0f, 60.0f);
UIImage *maskImage = [UIImage imageNamed:@"Meal"];
maskLayer.contents = (__bridge id)maskImage.CGImage;
//apply mask to image layer
mealView.layer.mask = maskLayer;
}
拉伸過濾
//minificationFilter:縮小圖片
//magnificationFilter:放大圖片
/*
CALayer為此提供了三種拉伸過濾方法,他們是:
kCAFilterLinear: 默認 雙線性濾波算法
kCAFilterNearest: 三線性濾波算法
kCAFilterTrilinear:最近過濾 就是取樣最近的單像素點而不管其他的顏色
*/
- (void)filterTest {
UIImage *digits = [UIImage imageNamed:@"Meal"];
//set up digit views
for (int k = 0; k < 6; k++) {
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0.0, 110 * k, 100.0f, 100.0f);
view.layer.contents = (__bridge id)digits.CGImage;
view.layer.contentsRect = CGRectMake(0.0, 0.0f, 0.1f, 1.0f);
view.layer.contentsGravity = kCAGravityResizeAspect;
//filter
view.layer.magnificationFilter = kCAFilterNearest;
[self.view addSubview:view];
}
timerDigit = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(tickDigit) userInfo:nil repeats:YES];
//set initial clock time
[self tickDigit];
}
- (void)setDigit:(NSInteger)digit forView:(UIView *)view {
//adjust contentsRect to select correct digit
view.layer.contentsRect = CGRectMake(digit * 0.1, 0, 0.1, 1.0);
}
- (void)tickDigit {
//convert time to hours, minutes and seconds
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUInteger units = NSCalendarUnitHour| NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
//set hours
[self setDigit:components.hour / 10 forView:digitViews[0]];
[self setDigit:components.hour % 10 forView:digitViews[1]];
//set minutes
[self setDigit:components.minute / 10 forView:digitViews[2]];
[self setDigit:components.minute % 10 forView:digitViews[3]];
//set second
[self setDigit:components.second / 10 forView:digitViews[4]];
[self setDigit:components.second % 10 forView:digitViews[5]];
}
組透明
//UIView有一個叫做alpha的屬性來確定視圖的透明度讶泰。CALayer有一個等同的屬性叫做opacity咏瑟,這兩個屬性都是影響子層級的
//設置了一個圖層的透明度,它包含的整個圖層樹像一個整體一樣的透明效果
//1種Info.plist文件中的UIViewGroupOpacity為YES來達到峻厚,但是這個設置會影響到這個應用响蕴,整個app可能會受到不良影響。
//2種設置CALayer的一個叫做shouldRasterize屬性
//為了啟用shouldRasterize屬性惠桃,我們設置了圖層的rasterizationScale屬性浦夷。默認情況下,所有圖層拉伸都是1.0辜王, 所以如果你使用了shouldRasterize屬性劈狐,你就要確保你設置了rasterizationScale屬性去匹配屏幕,以防止出現(xiàn)Retina屏幕像素化的問題呐馆。
//shouldRasterize和UIViewGroupOpacity一起的時候肥缔,性能問題就出現(xiàn)了
- (UIButton *)customButton {
//creat button
CGRect frame = CGRectMake(0, 0, 150, 150);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
button.backgroundColor = [UIColor redColor];
button.layer.cornerRadius = 10;
//add label
frame = CGRectMake(20, 10, 110, 30);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = @"Hello Word";
label.textAlignment = NSTextAlignmentCenter;
[button addSubview:label];
return button;
}
- (void)alphaTest {
//create opaque button
UIButton *button1 = [self customButton];
button1.center = CGPointMake(50, 150);
[self.view addSubview:button1];
//create translucent button
UIButton *button2 = [self customButton];
button2.center = CGPointMake(250, 150);
button2.alpha = 0.5;
[self.view addSubview:button2];
//enable rasterization for the translucent button
button2.layer.shouldRasterize = YES;
button2.layer.rasterizationScale = [UIScreen mainScreen].scale;
}