动画效果的三种方法代码段

本文介绍了在Objective-C中实现动画的三种方法,包括CATransition的高级控制,可以用于创建更多定制的动画效果,并能存储和复用。另外,还提到了UIView类方法的便捷性,尽管其过渡类型有限。CATransition在Mac OS X支持使用CoreImage过滤器,但在iOS上不支持。最后,文章讨论了UIView动画的简单实用场景,如淡入淡出、视图切换等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (IBAction)image:(id)sender {
    [UIView beginAnimations:@"xuanzhuan" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.myimage cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    if (self.myimage.image==[UIImage imageNamed:@"0.png"]) {
        [self.myimage setImage:[UIImage imageNamed:@"1413009041819.png"]];
    }
    else
        [self.myimage setImage:[UIImage imageNamed:@"0.png"]];
    [UIView commitAnimations];
}
- (IBAction)controller:(id)sender {//可以用到controller的切换
    CATransition *anima=[CATransition animation];//也可以使用alloc初始化
    [anima setDuration:1];
    [anima setType:kCATransitionMoveIn];//这个暂时不知道什么效果,去掉不可以
    [anima setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [self.view.layer addAnimation:anima forKey:kCATransitionReveal];
}
- (IBAction)qiehuan:(id)sender {
    CATransition *anima=[[CATransition alloc]init];
    [anima setDuration:1];
    //[anima setType:kCATransitionFromRight];
    if (self.myimage.image==[UIImage imageNamed:@"0.png"]) {
        [self.myimage setImage:[UIImage imageNamed:@"1413009041819.png"]];
    }
    else
        [self.myimage setImage:[UIImage imageNamed:@"0.png"]];
    [anima setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [self.myimage.layer addAnimation:anima forKey:kCATransitionPush];
}

CATransition方法给你更多的控制设置,因此更多的事情,例如。定时功能。作为一个对象,您可以将它存储为后,重构点所有动画,以减少重复代码,等等。      

UIView类方法是方便的方法常见的动画,但比CATransition更有限。例如,只有四种可能的过渡类型(翻转,翻转,蜷缩,旋度)。如果你想做一个淡入,要么你不得不掏腰包CATransition褪色的过渡,或者建立一个明确动画UIView的alpha。      

注意CATransition在Mac OS X将允许您指定一个任意CoreImage过滤器作为过渡,但目前iPhone现在你不能这么做,这缺乏CoreImage

CGRect imageFrame = imageView.frame;
imageFrame.origin.y = self.view.bounds.size.height;

[UIView animateWithDuration:0.5
    delay:1.0
    options: UIViewAnimationCurveEaseOut
    animations:^{
        imageView.frame = imageFrame;
    } 
    completion:^(BOOL finished){
        NSLog(@"Done!");
    }];

在使用后者对于很多不错的轻量级的动画。你可以用它淡入淡出两个视图,或消失在前面的另一个,或褪色。你可以拍一个视图在另一个像一个横幅,你可以查看伸展或收缩……我得到很多beginAnimation / commitAnimations里程。

不仅仅使用

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];

可以使用

[UIView beginAnimations:nil context:NULL]; {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    if (movingViewIn) {
// after the animation is over, call afterAnimationProceedWithGame
//  to start the game
        [UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];

//      [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation
//      [UIView setAnimationDelay:0.50];
//      [UIView setAnimationRepeatAutoreverses:YES];

        gameView.alpha = 1.0;
        topGameView.alpha = 1.0;
        viewrect1.origin.y = selfrect.size.height - (viewrect1.size.height);
        viewrect2.origin.y = -20;

        topGameView.alpha = 1.0;
    }
    else {
    // call putBackStatusBar after animation to restore the state after this animation
        [UIView setAnimationDidStopSelector:@selector(putBackStatusBar)];
        gameView.alpha = 0.0;
        topGameView.alpha = 0.0;
    }
    [gameView setFrame:viewrect1];
    [topGameView setFrame:viewrect2];

} [UIView commitAnimations];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值