~~静态的核心动画—阴影、边框
- (void)exampleOne {
//首先配置view的layer属性,展示一个带圆角的层
self.view.layer.backgroundColor = [UIColorgreenColor].CGColor;
self.view.layer.cornerRadius =20.0f;
self.view.layer.frame =CGRectInset(self.view.layer.frame,20, 20);
//再添加一个带阴影效果的子层
CALayer *subLayer = [CALayerlayer];
subLayer.backgroundColor = [UIColorpurpleColor].CGColor;
//对于图层阴影来说默认的有一点向上的偏移量 还有一定的扩散程度,淡然自己也可以设置
//设置阴影的偏移量
subLayer.shadowOffset =CGSizeMake(10,30);
//设置阴影的半径(是阴影的模糊的半径)
subLayer.shadowRadius =50.0f;
//设置阴影的颜色
subLayer.shadowColor = [UIColorblackColor].CGColor;
//阴影的默认的透明度是0,如果想要阴影显示出来必须设置阴影的透明度为非0
subLayer.shadowOpacity =1;
subLayer.frame =CGRectMake(30,30, 128, 192);
[self.view.layeraddSublayer:subLayer];
//为子层增加内容(图片),可以设置图层的边框
subLayer.contents = (id)[UIImageimageNamed:@"Snip20140923_7.png"].CGImage;
//边框的颜色CGColorRef 不可以直接用UIColor来创建对象
//可以用UIColor来创建对象,然后调CGColor来获得CGColorRef的内容
subLayer.borderColor = [UIColorredColor].CGColor;
//边框的宽度
subLayer.borderWidth =2.0;
subLayer.cornerRadius =50.0;
subLayer.masksToBounds =YES;
}
//我们可能需要视图有圆角和阴影的效果,用cornerRadius属性就行。实际上你就算设置了cornerRadius属性,图片仍然不会显示圆角。因为如果是这样,这个层的阴影显示就没有了。如何实现?
- (void)exampleTwo {
//配置子视图为阴影
CALayer *subLayer = [CALayerlayer];
subLayer.backgroundColor = [UIColorblueColor].CGColor;
subLayer.shadowOffset =CGSizeMake(0,3);
subLayer.shadowRadius =5.0;
subLayer.shadowColor = [UIColorblackColor].CGColor;
subLayer.shadowOpacity =0.8;
subLayer.frame =CGRectMake(30,30+192+20,128, 128);
subLayer.borderColor = [UIColorblackColor].CGColor;
subLayer.borderWidth =2.0;
//图层的拐角半径
subLayer.cornerRadius =10.0;
[self.view.layeraddSublayer:subLayer];
//在子层上添加一个圆角视图
CALayer *imageLayer = [CALayerlayer];
imageLayer.frame =CGRectMake(20,20, 60, 60);
imageLayer.cornerRadius =30.0;
imageLayer.contents = (id)[UIImageimageNamed:@“123.png"].CGImage;
imageLayer.masksToBounds =YES;
imageLayer.borderWidth =2.0f;
imageLayer.borderColor = [UIColorredColor].CGColor;
[subLayeraddSublayer:imageLayer];
}
~~简单动态的核心动画
- (void)changeByAnimation:(id)sender {
//animationWithKeyPath的值:
// transform.scale = 比例转换
// transform.scale.x = 宽的比例转换
// transform.scale.y = 高的比例转换
// transform.rotation.z = 平面图的旋转
// opacity = 透明度
CABasicAnimation *theAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.rotation.x"];
theAnimation.duration =3;
theAnimation.repeatCount =0;//默认的是 0,意味着动画只会播放一次。如果指定一个无限大的重复次数,使用 1e100f。这个不应该和 repeatDration 属性一块使用。
//repeatDration
//这个属性指定了动画应该被重复多久。动画会一直重复,直到设定的时间流逝完。它不应该和 repeatCount一起使用。
theAnimation.removedOnCompletion =FALSE;//这个属性默认为 YES,那意味着,在指定的时间段完成后,动画就自动的从层上移除了。这个一般不用。
//fillMode主要是决定显示layer在动画完成后的状态..一般和removedOnCompletion一起使用..
// 不管removedOnCompletion是yes还是no,都会回到原始状态..
// 如果..removedOnCompletion 是yes,动画完成后会回到原始状态..
// removedOnCompletion是NO的话..动画完成后会保持状态..
theAnimation.fillMode =kCAFillModeBoth;
theAnimation.autoreverses =YES;//当你设定这个属性为 YES时,在它到达目的地之后,动画的返回到开始的值,代替了直接跳转到开始的值。假如你想要再次用这个动画时,你需要设定这个属性为 NO。这样的话,下次你在通过-set方法设定动画的属 性时,它将再次使用你的动画,而非默认的动画。
theAnimation.fromValue = [NSNumbernumberWithFloat:M_PI /4];
theAnimation.byValue = [NSNumbernumberWithFloat:M_PI /2];
[_layer addAnimation:theAnimation forKey:@"layer_animation3"];
CABasicAnimation *theAnimation2=[CABasicAnimationanimationWithKeyPath:@"transform.scale"];
theAnimation2.duration =1;
theAnimation2.repeatCount =0;
theAnimation2.removedOnCompletion =FALSE;
theAnimation2.fillMode =kCAFillModeBoth;
theAnimation2.autoreverses =NO;
theAnimation2.toValue = [NSNumbernumberWithFloat:2];
[_layer2 addAnimation:theAnimation2 forKey:@"layer_animation4"];
}