//
// AppDelegate.m
// 不会说话的汤姆猫
//
// Created by qianfeng on 14-8-27.
// Copyright (c) 2014年 xuli. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
-(void)dealloc
{
[self.window release];
[super dealloc];
}
-(void)createImageView
{
//首先创建一个UIImageView的对象
UIImageView * imageView = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
//imageView.image =[UIImage imageNamed:@""];
//如果图片所占内存比较大 用以上方法添加的图片会发存放在应用程序中 占用系统的活跃内存 系统的活跃内存是有限的 所以存放图片如果很大 不建议使用以上方法
//NSBundle 路径类
//[NSBundle mainBundle] 获取的是当前工程的路径
//pathForResource:获取某个资源 第一个参数添加的是查找的资源的名称 第二个参数是资源的后缀名
NSString * path = [[NSBundle mainBundle] pathForResource:@"cat_eat0000" ofType:@"jpg"];
//每次使用的图片的时候都会直接到路径下获取 然后不存放在活跃内存中 虽然效率很低 但是很安全 不会因为获取内存不足报警告 或程序崩溃
UIImage * image1 = [UIImage imageWithContentsOfFile:path];
imageView.image = image1;
NSMutableArray * arr = [[NSMutableArray alloc]init];
//将所有图片添加到数组里
for(int i = 0;i<<span style="color: #272ad8">40;i++)
{
NSString * string = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"cat_eat00%.2d",i] ofType:@"jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:string];
[arr addObject:image];
}
[self.window addSubview:imageView];
//当前的imageView与用户进行交互
imageView.userInteractionEnabled = YES;
//UIImageVeiw 与UILabel 都是不能与用户交互 所以要将userInteractionEnabled设为yes以后才可进行交互
//1、为当前的imageView 添加一组图片(数组中存放的就是图片的对象指针)
[imageView setAnimationImages:arr];
//2、动画时间
[imageView setAnimationDuration:3];
//3、设置动画的显示次数(-1表示无限循环)
[imageView setAnimationRepeatCount:-1];
//4、动画开始
[imageView startAnimating];
//5、动画停止的方法
//点击手势类
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(stop:)];
//设置点击的次数
tap.numberOfTapsRequired = 1;
//几根手指点击
tap.numberOfTouchesRequired = 2;
//将点击事件添加到imageView上
[imageView addGestureRecognizer:tap];
}
-(void)stop:(UITapGestureRecognizer * )recognizer
{
//通过手势 查找添加了手势的视图
[((UIImageView *)recognizer.view) stopAnimating];
//stopAnimating停止动画
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self createImageView];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end