UIView動畫

UIView動畫
1、 commitAnimations方式使用UIView動畫
一、 commitAnimations方式使用UIView動畫
[UIView beginAnimations:@"animation" context:nil]; //UIView開始動畫,第一個參數是動畫的標識,第二個參數附加的應用程序信息用來傳遞給動畫代理消息
二、延時動畫時間(秒)
[UIView setAnimationDuration:1]; //動畫持續時間(秒)
三、設置動畫曲線,控制動畫速度
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
四、設置動畫方式,並指出動畫發生的位置
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 
五、 設置動畫代理
[UIView setAnimationDelegate:self];
六、 動畫結束後調用的方法,注意設置次方法以前要先設置代理
[UIView setAnimationDidStopSelector:@selector(stop)];
七、 提交動畫
[UIView commitAnimations];
2、交換本視圖控制器中 2個view位置
一、建立動畫
[UIView beginAnimations:@"animation" context:nil];
二、動畫持續時間
[UIView setAnimationDuration:1];
三、動畫曲線
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
四、交換位置
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];//交換位置
五、在 commitAnimations消息以前,能夠設置動畫完成後的回調,設置方法是:
[UIView setAnimationDidStopSelector:@selector(animationFinish:)];
3、使用: CATransition
一、建立動畫
CATransition *ansition = [CATransitionanimation];
二、動畫持續時間
ansition.duration = 2;
三、 動畫循環次數
ansition.repeatCount = 2;
四、 設置動畫類型
ansition.type = @"cameraIrisHollowOpen";
五、 設置動畫方向
ansition.subtype = kCATransitionFromLeft;
六、設置動畫代理
ansition.delegate = self;
七、替換
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
八、添加動畫
[view.layeraddAnimation:ansition forKey:@"12"];
九、 animateWithDuration
[UIView animateWithDuration:1 animations:^{
         //要執行的操做
         _view.frame = CGRectMake(20, 140, 200, 100);
        }];
       [UIView animateWithDuration:1 animations:^{
            //要執行的操做
        } completion:^(BOOL finished) {
            動畫結束
        }];
(1)transition.type 的類型能夠有
 淡化、推擠、揭開、覆蓋
NSString * const kCATransitionFade;
NSString * const kCATransitionMoveIn;
NSString * const kCATransitionPush;
NSString * const kCATransitionReveal;
 
(2)transition.subtype
 也有四種
NSString * const kCATransitionFromRight;
NSString * const kCATransitionFromLeft;
NSString * const kCATransitionFromTop;
NSString * const kCATransitionFromBottom;
(3)私有的類型的動畫類型:
animation.type = @」cube」
animation.type = @」suckEffect」
animation.type = @」oglFlip」//沒有方向
animation.type = @」rippleEffect」
animation.type = @」pageCurl」
animation.type = @」pageUnCurl」
animation.type = @」cameraIrisHollowOpen」
animation.type = @」cameraIrisHollowClose」
 
4、具體代碼
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(50, 50, 100, 30);
    //btn1.backgroundColor = [UIColor blackColor];
    [btn1 setTitle:@"動畫一" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btn1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
   
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(120, 50, 100, 30);
    [btn2 setTitle:@"動畫二" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btn2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
   
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(190, 50, 100, 30);
    [btn3 setTitle:@"動畫三" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(btn3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
   
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(375/2.0-50, 300, 100, 100)];
    view.tag = 1;
    view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view];
}

-(void)btn1{
    UIView *view = [self.view viewWithTag:1];
    [UIView beginAnimations:@"1" context:nil];//建立動畫
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];//動畫曲線
    [UIView setAnimationDelay:0];//延時動畫
    [UIView setAnimationDuration:2];//動畫持續時間
    [UIView setAnimationRepeatCount:2];//動畫循環次數
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];//設置動畫效果,以及要執行的view(動畫發生位置,此處是view)
    [UIView setAnimationDelegate:self];//設置動畫代理
    [UIView setAnimationDidStopSelector:@selector(stop)];//動畫結束後調用的方法,注意設置次方法以前要先設置代理
    [UIView commitAnimations];//提交動畫
   
}
-(void)btn2{
    UIView *view = [self.view viewWithTag:1];
    CATransition *ansition = [CATransition animation];//建立動畫
    ansition.duration = 2;//動畫持續時間
    //ansition.repeatCount = 2;//動畫循環次數
    ansition.type = @"cameraIrisHollowOpen";//設置動畫類型
    ansition.subtype = kCATransitionFromLeft;//設置動畫方向
    ansition.delegate = self;//設置動畫代理
    [view.layer addAnimation:ansition forKey:@"12"];//添加動畫
   
}
-(void)btn3{
    UIView *view = [self.view viewWithTag:1];
    CGPoint center = view.center;
    //放大效果
//    [UIView animateWithDuration:1 animations:^{
//        view.frame = CGRectMake(10, 10, 300, 300);
//        view.center = center;
//       
//    }];
    //放大縮小連續
    [UIView animateWithDuration:1 animations:^{
        view.frame = CGRectMake(10, 10, 300, 300);
        view.center = center;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            view.frame = CGRectMake(375/2.0, 300, 100, 100);
            view.center = center;
        } completion:^(BOOL finished) {
            [self btn3];//添加動畫
        }];
    }];
   
    //實現縮小和放大
    [UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        view.frame = CGRectMake(10, 10, 300, 300);
        view.center = center;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            view.frame = CGRectMake(375/2.0, 300, 100, 100);
            view.center = center;
        }completion:^(BOOL finished) {
            [self btn3];
        }];
    }];
   
}

-(void)stop1{
    NSLog(@"動畫中止");
}
-(void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"動畫開始");
}
相關文章
相關標籤/搜索