//圖片進行自動旋轉
CABasicAnimation是一個最多隻能有兩個主要畫面格的動畫,
UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"7"]];
imageView.frame =CGRectMake(40, 60,200, 250);
[self.viewaddSubview:imageView];
CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform"];
animation.delegate =self;
animation.toValue = [NSValuevalueWithCATransform3D:CATransform3DMakeRotation(M_PI,0, 0,1.0)];
//執行時間
animation.duration =30;
animation.cumulative =YES;//累積的
//執行次數
animation.repeatCount = INT_MAX;
animation.autoreverses=YES;//是否自動重複
[imageView.layeraddAnimation: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
{
[superviewDidLoad];
UIButton* button12 = [[UIButtonalloc]initWithFrame:CGRectMake(20,340, 150, 30)];
[button12 addTarget:self action:@selector(button22)forControlEvents:UIControlEventTouchUpInside];
[button12 setTitle:@"開始動畫" forState:UIControlStateNormal];
button12.backgroundColor = [UIColorredColor];
[self.viewaddSubview: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
{
//開始動畫
[UIViewbeginAnimations:@"testanimation"context:nil];
[UIViewsetAnimationDuration:0.5];
//代理
[UIViewsetAnimationDelegate:self];
//動畫的響應事件,使視圖自動回到原來的位置
[UIViewsetAnimationDidStopSelector:@selector(animationstop)];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
//獲得視圖的位置
CGRect frames =self.view_1.frame;
frames.origin.x =160;
self.view_1.frame = frames;
[UIViewcommitAnimations];
}
-(void)animationstop
{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.5];
CGRect frames =self.view_1.frame;
frames.origin.x =0;
self.view_1.frame = frames;
[UIViewcommitAnimations];
}