1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
blueView = UIView()
2
blueView.frame = CGRectMake(
50
,
100
,
100
,
100
)
3
blueView.backgroundColor = UIColor.blueColor()
4
self.view.addSubview(blueView)
5
6
yellowView = UIView()
7
yellowView.frame = CGRectMake(
50
+
200
/
1.414
,
100
,
100
,
100
)
//注意yellowView的水平位置,和blueView作对比
8
yellowView.backgroundColor = UIColor.yellowColor()
9
self.view.addSubview(yellowView)
10
11
blueView.transform = CGAffineTransformIdentity
//初始化transform
12
blueView.transform = CGAffineTransformMakeScale(
0.5
,
0.5
)
//缩小0.5倍
13
blueView.transform = CGAffineTransformRotate(blueView.transform, CGFloat(M_PI_4))
//旋转pi/4
14
blueView.transform = CGAffineTransformTranslate(blueView.transform,
400
,
0
)
//平移400
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
DEMO1:
1
let shapePath = UIBezierPath()
2
shapePath.moveToPoint(CGPointMake(
175
,
100
))
3
shapePath.addArcWithCenter(CGPointMake(
150
,
100
), radius:
25
, startAngle:
0
, endAngle:CGFloat(
2
* M_PI ), clockwise:
true
)
4
shapePath.moveToPoint(CGPointMake(
150
,
125
))
5
shapePath.addLineToPoint(CGPointMake(
150
,
175
))
6
shapePath.addLineToPoint(CGPointMake(
125
,
225
))
7
shapePath.moveToPoint(CGPointMake(
150
,
175
))
8
shapePath.addLineToPoint(CGPointMake(
175
,
225
))
9
shapePath.moveToPoint(CGPointMake(
100
,
150
))
10
shapePath.addLineToPoint(CGPointMake(
200
,
150
))
11
12
let shapeLayer = CAShapeLayer()
13
shapeLayer.strokeColor = UIColor.redColor().CGColor
14
shapeLayer.fillColor = UIColor.clearColor().CGColor
15
shapeLayer.lineWidth =
5
16
shapeLayer.lineJoin = kCALineJoinRound
17
shapeLayer.lineCap = kCALineCapRound
18
shapeLayer.path = shapePath.CGPath
19
self.view.layer.addSublayer(shapeLayer)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1
var textLayer = CATextLayer()
2
textLayer.frame = CGRectMake(
100
,
100
,
200
,
300
)
3
self.view.layer.addSublayer(textLayer)
4
5
//设置它的属性
6
textLayer.foregroundColor = UIColor.redColor().CGColor
7
textLayer.alignmentMode = kCAAlignmentJustified
8
textLayer.wrapped =
true
9
10
//它的font是CGFont类型,需要大小字体分开设置
11
var font = UIFont.systemFontOfSize(
24
)
12
textLayer.font = CGFontCreateWithFontName(font.fontName)
13
textLayer.fontSize = font.pointSize
14
15
var textStr =
"hello kitty hi nohhhh no wo shi lllll aaaaawo jiojhello kitty hi nohhhh no wo shi lllll aaaaawo jioj"
16
textLayer.string = textStr
17
18
//它contentScale默认是1,为了让它以retina的质量来显示,设置为2
19
textLayer.contentsScale = UIScreen.mainScreen().scale
|
1
2
3
4
5
6
7
8
|
1
gradientLayer = CAGradientLayer()
2
gradientLayer.frame = CGRectMake(
50
,
50
,
300
,
300
)
3
gradientLayer.backgroundColor = UIColor.grayColor().CGColor
4
self.view.layer.addSublayer(gradientLayer)
5
6
gradientLayer.colors = [UIColor.redColor().CGColor,UIColor.blueColor().CGColor, UIColor.greenColor().CGColor]
7
gradientLayer.startPoint = CGPointMake(
0
,
0
)
8
gradientLayer.endPoint = CGPointMake(
1
,
1
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
repeatLayer = CAReplicatorLayer()
2
repeatLayer.frame = CGRectMake(
50
,
50
,
300
,
300
)
3
repeatLayer.backgroundColor = UIColor.grayColor().CGColor
4
self.view.layer.addSublayer(repeatLayer)
5
6
var transform = CATransform3DIdentity
7
transform = CATransform3DTranslate(transform,
0
,
100
,
0
);
8
transform = CATransform3DRotate(transform, CGFloat( M_PI /
5.0
),
0
,
0
,
1
);
9
transform = CATransform3DTranslate(transform,
0
, -
100
,
0
);
10
repeatLayer.instanceTransform = transform;
11
12
repeatLayer.instanceCount =
10
13
14
repeatLayer.instanceBlueOffset = -
0.1
15
repeatLayer.instanceRedOffset = -
0.1
16
17
var layer = CALayer()
18
layer.frame = CGRect(x:
125
, y:
125
, width:
50
, height:
50
)
19
layer.backgroundColor = UIColor.whiteColor().CGColor
20
repeatLayer.addSublayer(layer)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
emitterLayer = CAEmitterLayer()
2
emitterLayer.frame = CGRectMake(
100
,
100
,
200
,
200
)
3
emitterLayer.backgroundColor = UIColor.grayColor().CGColor
4
self.view.layer.addSublayer(emitterLayer)
5
6
emitterLayer.renderMode = kCAEmitterLayerAdditive
7
emitterLayer.emitterPosition = CGPointMake(emitterLayer.frame.size.width /
2
, emitterLayer.frame.size.height /
2
)
8
9
var cell = CAEmitterCell()
10
cell.contents = UIImage(named:
"lizi.png"
)?.CGImage
11
cell.birthRate =
10
//粒子出现的速率
12
cell.lifetime =
4.0
//声明周期,秒
13
cell.emissionRange =
2
//发射的方向
14
15
cell.color = UIColor(red:
1
, green:
1
, blue:
0.5
, alpha:
1
).CGColor
//粒子的颜色
16
cell.alphaSpeed = -
0.4
//透明度改变速率
17
cell.velocity =
50
//粒子运动速度
18
cell.velocityRange =
100
//粒子速度范围,约束速度
19
20
emitterLayer.emitterCells = [cell]
|
1
2
3
4
5
6
7
8
9
10
|
1
var urlStr = NSBundle.mainBundle().pathForResource(
"1.mp4"
, ofType: nil)
2
var url = NSURL(fileURLWithPath: urlStr!)
3
var player = AVPlayer(URL: url)
4
5
var playLayer = AVPlayerLayer(player: player)
6
playLayer.frame = CGRectMake(
0
,
0
,
400
,
300
)
7
playLayer.backgroundColor = UIColor.grayColor().CGColor
8
self.view.layer.addSublayer(playLayer)
9
10
player.play()
|