IOS汤姆猫小练习

本文介绍了一个使用Objective-C编写的iOS应用程序中Tom动画的具体实现方法。该应用通过加载一系列图片来实现动画效果,并提供了触摸头部和吃鸟两个动画触发事件。文章详细展示了如何创建图片数组、设置动画参数以及执行动画。

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

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tomImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)touchHeadOnclick:(id)sender {
    [self tomAnimationWith:@"knockout" imageCount:81];
}

- (IBAction)eatBirdOnclick:(id)sender {
    [self tomAnimationWith:@"eat" imageCount:40];
}

-(void) tomAnimationWith:(NSString*)imageFile imageCount:(int)imageCount {
    if (self.tomImageView.isAnimating) {
        return;
    }
    
    // 1首先创建图片数组
    NSMutableArray *tomImages = [NSMutableArray array];
    for (int i = 0; i < imageCount; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", imageFile, i];
        // 图片过多的时候容易造成内存溢出
        // UIImage *image = [UIImage imageNamed:imageName];
        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
        UIImage * image = [UIImage imageWithContentsOfFile:path];
        
        [tomImages addObject:image];
    }
    
    // 2设置动画过程
    // 2.1添加动画执行的图片数组
    [self.tomImageView setAnimationImages:tomImages];
    // 2.2设置动画执行的时间
    [self.tomImageView setAnimationDuration:self.tomImageView.animationImages.count * 0.1];
    // 2.3设置动画执行次数
    [self.tomImageView setAnimationRepeatCount:1];
    // 设置开始执行动画
    [self.tomImageView startAnimating];
    // 3在动画结束之后清空数据
    //[self performSelector:@selector(clearImages) withObject:nil afterDelay:self.tomImageView.animationDuration];
    [self.tomImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tomImageView.animationDuration];
}

//- (void) clearImages {
//    self.tomImageView.animationImages = nil;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值