imageVIew自带的动画效果(类似TOM…

来源:互联网 发布:mac 安装lnmp php7 编辑:程序博客网 时间:2024/06/05 11:28

//

//  AppDelegate.m

// 不会说话的汤姆猫

//

//  Created by qianfeng on 14-8-27.

//  Copyright (c) 2014xuli. 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 =[UIImageimageNamed:@""];

   //如果图片所占内存比较大用以上方法添加的图片会发存放在应用程序中 占用系统的活跃内存 系统的活跃内存是有限的 所以存放图片如果很大 不建议使用以上方法

   //NSBundle 路径类

   //[NSBundle mainBundle] 获取的是当前工程的路径

   //pathForResource:获取某个资源第一个参数添加的是查找的资源的名称 第二个参数是资源的后缀名

   NSString *path = [[NSBundlemainBundle]pathForResource:@"cat_eat0000" ofType:@"jpg"];

   //每次使用的图片的时候都会直接到路径下获取然后不存放在活跃内存中 虽然效率很低 但是很安全 不会因为获取内存不足报警告 或程序崩溃

    UIImage * image1 = [UIImage imageWithContentsOfFile:path];

    

    imageView.image = image1;

    

   NSMutableArray * arr = [[NSMutableArray alloc]init];

   //将所有图片添加到数组里

    for(inti = 0;i<<spanstyle="color: #272ad8">40;i++)

    {

       NSString *string = [[NSBundlemainBundle]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 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(stop:)];

    

   //设置点击的次数

   tap.numberOfTapsRequired = 1;

   //几根手指点击

   tap.numberOfTouchesRequired = 2;

   //将点击事件添加到imageView

    [imageView addGestureRecognizer:tap];

    

    

    

}

-(void)stop:(UITapGestureRecognizer * )recognizer

{

   //通过手势查找添加了手势的视图

    [((UIImageView *)recognizer.view) stopAnimating];

   //stopAnimating停止动画

}


- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   // Override point for customization afterapplication launch.

   self.window.backgroundColor = [UIColor whiteColor];

   [selfcreateImageView];

   [self.windowmakeKeyAndVisible];

   return YES;

}


- (void)applicationWillResignActive:(UIApplication*)application

{

   // Sent when the application is about tomove from active to inactive state. This can occur for certaintypes of temporary interruptions (such as an incoming phone call orSMS message) or when the user quits the application and it beginsthe transition to the background state.

   // Use this method to pause ongoingtasks, 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 sharedresources, save user data, invalidate timers, and store enoughapplication state information to restore your application to itscurrent state in case it is terminated later. 

   // If your application supportsbackground execution, this method is called instead ofapplicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication*)application

{

   // Called as part of the transition fromthe background to the inactive state; here you can undo many of thechanges made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication*)application

{

   // Restart any tasks that were paused (ornot yet started) while the application was inactive. If theapplication was previously in the background, optionally refreshthe user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application

{

   // Called when the application is aboutto terminate. Save data if appropriate. See alsoapplicationDidEnterBackground:.

}


@end


0 0
原创粉丝点击