shadowOpacity属性,设置阴影是在0.0到1.0之间的浮点数
若要改变阴影可以用这三个属性 shadowColor,shadowOffset和shadowRadius
其中shadowColor是阴影颜色,默认为黑色,(其他颜色怪怪的)
shadowOffset控制阴影的方向和距离(默认值是0,-3)既是向上3个单位
shadowRadius控制阴影的模糊度,其中不可以设置 masksToBounds为YES
如果要设置圆角,又要裁剪这个时候就要再父视图上再添加一个视图,再将你要显示的加载再这个视图上,
类似于cell的contentView.
borderWidth,borderColor属性,边框的宽度个边框的颜色
其中 borderColor是CGColor的
图层蒙版
一个实例
@end
CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)
//平移
//更深一层的变换
CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty)
一个实例
}
若要改变阴影可以用这三个属性 shadowColor,shadowOffset和shadowRadius
其中shadowColor是阴影颜色,默认为黑色,(其他颜色怪怪的)
shadowOffset控制阴影的方向和距离(默认值是0,-3)既是向上3个单位
shadowRadius控制阴影的模糊度,其中不可以设置 masksToBounds为YES
如果要设置圆角,又要裁剪这个时候就要再父视图上再添加一个视图,再将你要显示的加载再这个视图上,
类似于cell的contentView.
borderWidth,borderColor属性,边框的宽度个边框的颜色
其中 borderColor是CGColor的
图层蒙版
一个实例
- (void)viewDidLoad
{
[
super
viewDidLoad];
//create mask layer
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = self.layerView.bounds;
UIImage *maskImage = [UIImage imageNamed:@
"Cone.png"
];
maskLayer.contents = (__bridge id)maskImage.CGImage;
//apply mask to image layer?
self.imageView.layer.mask = maskLayer;
}
CGAffineTransformMakeRotation(CGFloat angle) //旋转
CGAffineTransformMakeScale(CGFloat sx, CGFloat sy) //缩放
//更深一层的变换
CGAffineTransformRotate(CGAffineTransform t, CGFloat angle)
CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)
一个实例
- (void)viewDidLoad
{
[
super
viewDidLoad];
//create a new transform
CGAffineTransform transform = CGAffineTransformIdentity;
//scale by 50%
transform = CGAffineTransformScale(transform, 0.5, 0.5);
//rotate by 30 degrees
transform = CGAffineTransformRotate(transform, M_PI / 180.0 * 30.0);
//translate by 200 points
transform = CGAffineTransformTranslate(transform, 200, 0);
//apply transform to layer
self.layerView.layer.affineTransform = transform;
变换的顺序会影响最终的结果,也就是说旋转之后的平移和平移之后的旋转结果可能不同
水平方向的斜切变换
}
//3D变换
CATransform3DMakeTranslation(Gloat tx, CGFloat ty, CGFloat tz)
//透视之后的y轴变换
}
另外一个实例
}
//创建一个立方体
//faces是一个数组
//containerView 是一个UIView
}
做完上述操作之后再次添加下面两行代码
perspective = CATransform3DRotate(perspective, -M_PI_4, 0, 1, 0);
之后你还需要添加阴影与高亮完整代码如下
需要引入系统的GLKit库
再.m中加入 #import <GLKit/GLKit.h>头文件
}
水平方向的斜切变换
CGAffineTransform CGAffineTransformMakeShear(CGFloat x, CGFloat y)
{
CGAffineTransform transform = CGAffineTransformIdentity;
transform.c = -x;
transform.b = y;
return
transform;
}
- (void)viewDidLoad
{
[
super
viewDidLoad];
//shear the layer at a 45-degree angle
self.layerView.layer.affineTransform = CGAffineTransformMakeShear(1, 0);
//3D变换
CATransform3DMakeRotation(CGFloat angle, CGFloat x, CGFloat y, CGFloat z)
CATransform3DMakeScale(CGFloat sx, CGFloat sy, CGFloat sz)
//透视之后的y轴变换
- (void)viewDidLoad
{
[
super
viewDidLoad];
//create a new transform
CATransform3D transform = CATransform3DIdentity;
//apply perspective
transform.m34 = - 1.0 / 500.0;
//rotate by 45 degrees along the Y axis
transform = CATransform3DRotate(transform, M_PI_4, 0, 1, 0);
//apply to layer
self.layerView.layer.transform = transform;
另外一个实例
- (void)viewDidLoad
{
[
super
viewDidLoad];
//rotate the outer layer 45 degrees
CATransform3D outer = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
self.outerView.layer.transform = outer;
//rotate the inner layer -45 degrees
CATransform3D inner = CATransform3DMakeRotation(-M_PI_4, 0, 0, 1);
self.innerView.layer.transform = inner;
//创建一个立方体
//faces是一个数组
//containerView 是一个UIView
- (void)addFace:(NSInteger)index withTransform:(CATransform3D)transform
{
//get the face view and add it to the container
UIView *face = self.faces[index];
[self.containerView addSubview:face];
//center the face view within the container
CGSize containerSize = self.containerView.bounds.size;
face.center = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
// apply the transform
face.layer.transform = transform;
}
- (void)viewDidLoad
{
[
super
viewDidLoad];
//set up the container sublayer transform
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1.0 / 500.0;
self.containerView.layer.sublayerTransform = perspective;
//add cube face 1
CATransform3D transform = CATransform3DMakeTranslation(0, 0, 100);
[self addFace:0 withTransform:transform];
//add cube face 2
transform = CATransform3DMakeTranslation(100, 0, 0);
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
[self addFace:1 withTransform:transform];
//add cube face 3
transform = CATransform3DMakeTranslation(0, -100, 0);
transform = CATransform3DRotate(transform, M_PI_2, 1, 0, 0);
[self addFace:2 withTransform:transform];
//add cube face 4
transform = CATransform3DMakeTranslation(0, 100, 0);
transform = CATransform3DRotate(transform, -M_PI_2, 1, 0, 0);
[self addFace:3 withTransform:transform];
//add cube face 5
transform = CATransform3DMakeTranslation(-100, 0, 0);
transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);
[self addFace:4 withTransform:transform];
//add cube face 6
transform = CATransform3DMakeTranslation(0, 0, -100);
transform = CATransform3DRotate(transform, M_PI, 0, 1, 0);
[self addFace:5 withTransform:transform];
做完上述操作之后再次添加下面两行代码
perspective = CATransform3DRotate(perspective, -M_PI_4, 1, 0, 0);
之后你还需要添加阴影与高亮完整代码如下
需要引入系统的GLKit库
再.m中加入 #import <GLKit/GLKit.h>头文件
#define LIGHT_DIRECTION 0, 1, -0.5
#define AMBIENT_LIGHT 0.5
- (void)applyLightingToFace:(CALayer *)face
{
//add lighting layer
CALayer *layer = [CALayer layer];
layer.frame = face.bounds;
[face addSublayer:layer];
//convert the face transform to matrix
//(GLKMatrix4 has the same structure as CATransform3D)
CATransform3D transform = face.transform;
GLKMatrix4 matrix4 = *(GLKMatrix4 *)&transform;
GLKMatrix3 matrix3 = GLKMatrix4GetMatrix3(matrix4);
//get face normal
GLKVector3 normal = GLKVector3Make(0, 0, 1);
normal = GLKMatrix3MultiplyVector3(matrix3, normal);
normal = GLKVector3Normalize(normal);
//get dot product with light direction
GLKVector3 light = GLKVector3Normalize(GLKVector3Make(LIGHT_DIRECTION));
float dotProduct = GLKVector3DotProduct(light, normal);
//set lighting layer opacity
CGFloat shadow = 1 + dotProduct - AMBIENT_LIGHT;
UIColor *color = [UIColor colorWithWhite:0 alpha:shadow];
layer.backgroundColor = color.CGColor;
}
- (void)addFace:(NSInteger)index withTransform:(CATransform3D)transform
{
//get the face view and add it to the container
UIView *face = self.faces[index];
[self.containerView addSubview:face];
//center the face view within the container
CGSize containerSize = self.containerView.bounds.size;
face.center = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
// apply the transform
face.layer.transform = transform;
//apply lighting
[self applyLightingToFace:face.layer];
}
- (void)viewDidLoad
{
[
super
viewDidLoad];
//set up the container sublayer transform
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1.0 / 500.0;
perspective = CATransform3DRotate(perspective, -M_PI_4, 1, 0, 0);
perspective = CATransform3DRotate(perspective, -M_PI_4, 0, 1, 0);
self.containerView.layer.sublayerTransform = perspective;
//add cube face 1
CATransform3D transform = CATransform3DMakeTranslation(0, 0, 100);
[self addFace:0 withTransform:transform];
//add cube face 2
transform = CATransform3DMakeTranslation(100, 0, 0);
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
[self addFace:1 withTransform:transform];
//add cube face 3
transform = CATransform3DMakeTranslation(0, -100, 0);
transform = CATransform3DRotate(transform, M_PI_2, 1, 0, 0);
[self addFace:2 withTransform:transform];
//add cube face 4
transform = CATransform3DMakeTranslation(0, 100, 0);
transform = CATransform3DRotate(transform, -M_PI_2, 1, 0, 0);
[self addFace:3 withTransform:transform];
//add cube face 5
transform = CATransform3DMakeTranslation(-100, 0, 0);
transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);
[self addFace:4 withTransform:transform];
//add cube face 6
transform = CATransform3DMakeTranslation(0, 0, -100);
transform = CATransform3DRotate(transform, M_PI, 0, 1, 0);
[self addFace:5 withTransform:transform];