UIView*aView=[self.view viewWithTag:100];
//開始動畫,第一個參數表示名字(兩個動畫公用一個代理時用來區分),第二個參數是上下文,在animationWillStart:context:方法中能夠接收這個參數。
[UIView beginAnimations:nil context:@"ddd"];
//設置動畫代理
[UIView setAnimationDelegate:self];
//動畫結束後調用的方法
[UIView setAnimationDidStopSelector:@selector(finsh:)];
//設置動畫時長
[UIView setAnimationDuration:1.0];
//動畫次數
[UIView setAnimationRepeatCount:2];
//自動返回
[UIView setAnimationRepeatAutoreverses:YES];
//漸變效果
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//動畫過程中視圖的透明度,可做呼吸效果
aView.alpha=0.1;
//動畫過程中修改顏色
aView.backgroundColor=[UIColor redColor];
//動畫移動
aView.frame=CGRectMake(100, 150, 100, 100);
//結束動畫
[UIView commitAnimations];
也可以在點擊按鈕觸發的方法中使用block語法:
<span style="font-size:18px;">-(void)buttonBlock:(UIButton*)button{
UIView*view=[self.view viewWithTag:100];
//使用block語法 結束時回調
[UIView animateWithDuration:2.0 animations:^{
view.backgroundColor=[UIColor redColor];
view.frame=CGRectMake(100, 150, 100, 100);
}completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}</span>
UIView翻轉動畫:
-(void)button1:(UIButton*)btu{
//做翻轉動畫
UIView*view=[self.view viewWithTag:100];
[UIView beginAnimations:nil context:NULL];
//cache是生成緩存,賦值yes後以後加載更快
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];
[UIView setAnimationDuration:2.0];
[UIView setAnimationRepeatCount:2];
[UIView commitAnimations];
}