注:动画的缺点1:动画执行完毕之后,会反弹到最初状态。
- 创建CABasicAnimation动画对象
// 创建核心动画对象
CABasicAnimation *anima = [CABasicAnimation animation];
// 通过keyPath来告诉图层执行怎样的动画(平移,旋转,缩放等)
// anima.keyPath = @"transform.rotation.z";
// anima.keyPath = @"transform.translation.y";
anima.keyPath = @"transform.scale.x";
// 到哪个位置停止动画
anima.fromValue = @(0.5);
// anima.toValue = @(M_PI_4);
anima.toValue = @(2);
// 设置动画执行时间 默认时间是0.25
anima.duration = 1;
// 延时执行动画
// CACurrentMediaTime:获得当前动画执行时间
anima.beginTime = CACurrentMediaTime() + 2;
// 保持动画执行完毕的状态
anima.removedOnCompletion = NO;
anima.fillMode = kCAFillModeBoth;
// 将动画对象添加到图层上
[self.redView.layer addAnimation:anima forKey:nil];
fillMode 的作用就是决定layer对象执行完动画后的行为。该属性只有在removedOnCompletion
设置为NO才会有效。
- kCAFillModeForwards:当动画结束后,layer会一直保持着动画最后的状态
kCAFillModeBackwards:只要动画被加入了layer,layer便立刻处于动画初始状态。将一个动画加入一个layer的时候延迟5秒执行即可测试出来。设置核心动画延时
anim.beginTime = CACurrentMediaTime() + 2;
其中CACurrentMediaTime获得当前动画时间。这里表示延时两秒。kCAFillModeBoth:动画加入layer后,在开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态。
kCAFillModeRemoved: 默认值。动画结束后,layer会恢复到之前的状态。
平移动画
- (void)translation {
CABasicAnimation *anima = [CABasicAnimation animation];
// 通过keyPath来告诉图层平移
anima.keyPath = @"position";
// 从哪个位置开始执行动画
anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
// 到哪个位置停止动画
// anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)];
// 增加到多少
anima.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)]; // (300,500)
缩放
- (void)scale {
CABasicAnimation *anima = [CABasicAnimation animation];
anima.keyPath = @"bounds"; // CGRect
anima.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)];
anima.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
旋转
- (void)rotation {
CABasicAnimation *anima = [CABasicAnimation animation];
anima.keyPath = @"transform";
anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
// 设置动画执行时间 默认时间是0.25
anima.duration = 1;
// 将动画对象添加到图层上
[self.redView.layer addAnimation:anima forKey:nil];
}
创建转场动画对象
CATransition *anim = [CATransition animation];
// 切换图片
NSString *imageName = [NSString stringWithFormat:@"%zd",self.currentIndex];
self.iconView.image = [UIImage imageNamed:imageName];
// 设置动画类型
anim.type = @"oglFlip";
// 设置动画子类型:过度方向
anim.subtype = kCATransitionFromRight;
// 将动画添加到图层上
[self.iconView.layer addAnimation:anim forKey:nil];
/* 过渡效果 */
// fade //交叉淡化过渡(不支持过渡方向) kCATransitionFade
// push //新视图把旧视图推出去 kCATransitionPush
// moveIn //新视图移到旧视图上面 kCATransitionMoveIn
// reveal //将旧视图移开,显示下面的新视图 kCATransitionReveal
// /******下面几个也是过渡效果,但它们是私有API***/
// cube //立方体翻滚效果
// oglFlip //上下左右翻转效果
// suckEffect //收缩效果,如一块布被抽走(不支持过渡方向)
// rippleEffect //滴水效果(不支持过渡方向)
// pageCurl //向上翻页效果
// pageUnCurl //向下翻页效果
// cameraIrisHollowOpen //相机镜头打开效果(不支持过渡方向)
// cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向)
获得当前时间
NSDate *date = [NSDate date];
// 获得日历对象
NSCalendar *calendar = [NSCalendar currentCalendar];
// 获得日期组件对象
NSDateComponents *component = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
// 获得时分秒
NSInteger hour = component.hour; // 4 * 30 = 120
NSInteger minute = component.minute; // 30 * 6 30 * 0.5 = 15
NSInteger second = component.second; // 30 * 6