能用到的各种简单的动画效果
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
MainViewController *mainVC = [[MainViewController alloc]init];
self.window.rootViewController = mainVC;
[mainVC release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 20, 100, 100);;
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//CALayer : 是一个图层类,负责显示UIView的形状
button.layer.cornerRadius = 60;
//边框宽度
button.layer.borderWidth = 10;
//边框颜色
button.layer.borderColor = [UIColor yellowColor].CGColor;
// button.layer.shadowColor = [UIColor grayColor].CGColor;
// button.layer.shadowOffset = CGSizeMake(10, 10);
button.layer.shadowRadius = 30;
// button.layer.shadowOpacity = 1;
}
- (void)buttonAction:(UIButton *)button
{
NSLog(@"press");
// //动画
// //参数一:动画执行的时间
// //参数二:block 回车就行 写动画的最终结束状态
// [UIView animateWithDuration:1 animations:^{
//动画的延时时间秒
// [UIView setAnimationDelay:2];
//动画重复
[UIView setAnimationRepeatAutoreverses:YES];
//重复次数
[UIView setAnimationRepeatCount:2];
// // [UIView setAnimationRepeatCount:NSIntegerMax];
// //速度曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
// //动画之后的颜色是黄色的
// button.backgroundColor = [UIColor yellowColor];
// //改变大小位置
// button.frame = CGRectMake(100, 100, 200, 300);
// //使用UIView动画能改变的属性
// //background frame center bounds alpha hidden transform(旋转)
// button.frame = self.view.bounds;
// }];
// [UIView animateWithDuration:1 animations:^{
// button.frame = CGRectMake(100, 100, 200, 300);
// } completion:^(BOOL finished) {
// NSLog(@"动画执行结束");可写请求之后要干什么事
// }];
// [UIView animateWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>];
//参数3:阻力 阻力大弹得幅度小(0-1)
//参数4:速度 (0-1)
// [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
// button.frame = CGRectMake(100, 100, 200, 300);
// } completion:^(BOOL finished) {
// NSLog(@"动画执行结束");
// self.view.backgroundColor = [UIColor yellowColor];
// }];
// [UIView animateWithDuration:1 animations:^{
// [UIView setAnimationCurve: UIViewAnimationCurveLinear];
// [UIView setAnimationRepeatCount:NSIntegerMax];
// button.transform = CGAffineTransformRotate(button.transform, M_PI);
// }];
//UIView 过度动画
// [UIView transitionWithView:button duration:1 options: UIViewAnimationOptionTransitionFlipFromBottom animations:^{
// button.frame = CGRectMake(20, 20, 300, 500);
// } completion:^(BOOL finished) {
self.view.backgroundColor = [UIColor yellowColor];
// }];
//按按钮加新的view 这个方法执行结束,参数1的view会从视图移除,参数2 的view会加到父视图上
// UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 334, 200)];
// view.backgroundColor = [UIColor greenColor];
// [UIView transitionFromView:button toView:view duration:1 options:UIViewAnimationOptionLayoutSubviews completion:^(BOOL finished) {
//
// }];
//CAAnimation
//1. CABasicAnimation
CABasicAnimation *ani1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//scale 缩放
//指定值的范围 初始值
ani1.fromValue = @(1);//强转为id对象类型
//目标值
ani1.toValue = @(2);
//动画执行的时间
ani1.duration = 1;
//反转
ani1.autoreverses = YES;
//重复次数
ani1.repeatCount = NSIntegerMax;
// [button.layer addAnimation:ani1 forKey:nil];
//旋转
CABasicAnimation *ani2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
ani2.fromValue = @(0);
ani2.toValue = @(2*M_PI);
ani2.duration = 1;
ani2.repeatCount = NSIntegerMax;
// [button.layer addAnimation:ani2 forKey:nil];
//2.组动画 CAAnimationGroup
CAAnimationGroup *group = [CAAnimationGroup animation];
//组动画可以把一系列的动画对象放到数组中
group.animations = @[ani1,ani2];
group.duration = 1;
group.autoreverses = YES;
group.repeatCount = NSIntegerMax;
// [button.layer addAnimation:group forKey:nil];
// 3. 关键帧动画
CAKeyframeAnimation *keyAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
//设置路径的起始点
CGPathMoveToPoint(path, NULL, 50, 100);
//加一条直线
CGPathAddLineToPoint(path, NULL, 10, 200);
CGPathAddLineToPoint(path, NULL, 100, 50);
CGPathAddLineToPoint(path, NULL, 20, 120);
//添加一条曲线
CGPathAddCurveToPoint(path, NULL, 30, 50, 120, 80, 40, 60);
CGPathAddCurveToPoint(path, NULL, 50, 150, 20, 10, 40, 80);
CGPathAddCurveToPoint(path, NULL, 30, 40, 120, 80, 40, 70);
CGPathAddCurveToPoint(path, NULL, 30, 150, 10, 80, 20, 60);
CGPathAddCurveToPoint(path, NULL, 130, 150, 10, 80, 50, 60);
//加一条曲线路径
CGPathAddCurveToPoint(path, NULL, 100, 20, 20, 200, 300, 100);
CGPathAddCurveToPoint(path, NULL, 10, 200, 10, 250, 30, 110);
// CGPathAddEllipseInRect(<#CGMutablePathRef path#>, <#const CGAffineTransform *m#>, <#CGRect rect#>)
keyAni.path = path;
keyAni.duration = 2;
// [button.layer addAnimation:keyAni forKey:nil];
//4. CATransition 过渡动画
CATransition *ani4 = [CATransition animation];
ani4.duration = 1;
ani4.type = @"cube" ;
ani4.subtype = kCATransitionFromLeft;
[button.layer addAnimation:ani4 forKey:nil];
/** type
*
* 各种动画效果 其中除了'fade', `moveIn', `push' , `reveal' ,其他属于私有的API.
* ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
* @"cube" 立方体翻滚效果
* @"moveIn" 新视图移到旧视图上面
* @"reveal" 显露效果(将旧视图移开,显示下面的新视图)
* @"fade" 交叉淡化过渡(不支持过渡方向) (默认为此效果)
* @"pageCurl" 向上翻一页
* @"pageUnCurl" 向下翻一页
* @"suckEffect" 收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
* @"rippleEffect" 滴水效果,(不支持过渡方向)
* @"oglFlip" 上下左右翻转效果
* @"rotate" 旋转效果
* @"push"
* @"cameraIrisHollowOpen" 相机镜头打开效果(不支持过渡方向)
* @"cameraIrisHollowClose" 相机镜头关上效果(不支持过渡方向)
*/
/** type
*
* kCATransitionFade 交叉淡化过渡
* kCATransitionMoveIn 新视图移到旧视图上面
* kCATransitionPush 新视图把旧视图推出去
* kCATransitionReveal 将旧视图移开,显示下面的新视图
*/
}