iOS开发之UI基础--三种简单的动画设置

本文介绍了iOS开发中的三种动画实现方式:首尾式动画、block动画及序列帧动画,并提供了详细的示例代码。通过这些示例,读者可以了解如何在实际项目中应用不同类型的动画。

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

一、首尾式动画

示例代码:


// beginAnimations表示此后的代码要“参与到”动画中

    [UIView beginAnimations:nil context:nil];
//设置动画时长
    [UIView setAnimationDuration:2.0];
    
      self.headImageView.bounds = rect;

    // commitAnimations,将beginAnimation之后的所有动画提交并生成动画
    [UIView commitAnimations];

二.block动画
示例代码:

//简单的动画效果
    [UIView animateWithDuration:2.0 animations:^{
        image1.alpha=0;
    } completion:^(BOOL finished) {
        [image1 removeFromSuperview];
    }];
//1.<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在实际的开发中更常用的时block代码块来处理动画操作。</span>
<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">//2.块动画相对来说比较灵活,尤为重要的是能够将动画相关的代码编写在一起,便于代码的阅读和理解.</span>


三.序列帧动画

1.示例代码:

NSMutableArray  *arrayM=[NSMutableArray array];
    for (int i=0; i<40; i++) {
        [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"eat_%02d.jpg",i]]];
    }
    //设置动画数组
    [self.tom setAnimationImages:arrayM];
    //设置动画播放次数
    [self.tom setAnimationRepeatCount:1];
    //设置动画播放时间
    [self.tom setAnimationDuration:40*0.075];
    //开始动画
    [self.tom startAnimating];

2. UIImageView的序列帧动画(需要考虑程序性能,释放数据)

// 0. 是否正在动画

[self.tom isAnimating];

// 1. 设置图片的数组

[self.tom setAnimationImages:arrayM];

// 2. 设置动画时长,默认每秒播放30张图片

[self.tom setAnimationDuration:arrayM.count * 0.075];

// 3. 设置动画重复次数,默认为0,无限循环

[self.tom setAnimationRepeatCount:1];

// 4. 开始动画

[self.tom startAnimating];

// 5. 动画播放完成后,清空动画数组

[self.tom performSelector:@selector(setAnimationImages:) withObject:nilafterDelay:self.tom.animationDuration];






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值