//圖片進行自動旋轉
CABasicAnimation是一個最多只能有兩個關鍵幀的動畫,
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"7"]];
imageView.frame = CGRectMake(40, 60, 200, 250);
[self.view addSubview:imageView];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.delegate = self;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1.0)];
//執行時間
animation.duration = 30;
animation.cumulative = YES;//累積的
//執行次數
animation.repeatCount = INT_MAX;
animation.autoreverses=YES;//是否自動重復
[imageView.layer addAnimation:animation forKey:@"animation"];
通過CATransition實現類似UINavigationController的pushview及Popview效果
CATransition動畫使用了類型type和子類型subtype兩個概念。type屬性指定了過渡的種類(淡化、推擠、揭開、覆蓋)。subtype設置了過渡的方向(從上、下、左、右)。另外,CATransition私有的動畫類型有(立方體、吸收、翻轉、波紋、翻頁、反翻頁、鏡頭開、鏡頭關)。
/* CATransition *myT = [ CATransition animation];
myT.timingFunction = UIViewAnimationCurveEaseInOut;
myT.type = @"cube";
//動畫類型
// 1 kCATransitionFromBottom
// 2 kCATransitionFromLeft
// 3 kCATransitionFromTop
// 4 kCATransitionFromRight
//myT.subtype = kCATransitionFromLeft;
myT.subtype = kCATransitionFromLeft;
myT.duration = 0.5;
[self.navigationController.view.layer addAnimation: myT forKey:nil];
NSLog(@"app");*/
創建UIView動畫(塊)――(指過渡效果的動畫)
//方法一:
//過度動畫的效果,是2個View的切換
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* button12 = [[UIButton alloc]initWithFrame:CGRectMake(20, 340, 150, 30)];
[button12 addTarget:self action:@selector(button22) forControlEvents:UIControlEventTouchUpInside];
[button12 setTitle:@"開始動畫" forState:UIControlStateNormal];
button12.backgroundColor = [UIColor redColor];
[self.view addSubview:button12];
//方法二:
// 2 用block語法實現
/* [UIView animateWithDuration:0.5 animations:^{
CGRect frames = self.view_1.frame;
frames.origin.x = 160;
self.view_1.frame = frames;
//縮放
self.view_1.transform = CGAffineTransformScale(self.view_1.transform, 0.01, 0.01);
}completion:^(BOOL finished) {
CGRect frames = self.view_1.frame;
frames.origin.x = 0;
self.view_1.frame = frames;
//恢復到原始的縮放
self.view_1.transform = CGAffineTransformIdentity;
}];*/
}
-(void)button22
{
//開始動畫
[UIView beginAnimations:@"testanimation" context:nil];
[UIView setAnimationDuration:0.5];
//代理
[UIView setAnimationDelegate:self];
//動畫的響應事件,使視圖自動回到原來的位置
[UIView setAnimationDidStopSelector:@selector(animationstop)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//獲得視圖的位置
CGRect frames = self.view_1.frame;
frames.origin.x = 160;
self.view_1.frame = frames;
[UIView commitAnimations];
}
-(void)animationstop
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect frames = self.view_1.frame;
frames.origin.x = 0;
self.view_1.frame = frames;
[UIView commitAnimations];
}