UIImageView 讲解以及使用

//
//  ZKAppDelegate.m
//  03-UIImgeView
//

#import "ZKAppDelegate.h"

@implementation ZKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    //UIImageview 显示图片的视图

    CGRect rect = [[UIScreen mainScreen] bounds];
    //1.加载背景图片
    UIImageView *background = [[UIImageView alloc]initWithFrame:rect];
    //jpg格式的图片,添加是要带着后缀名  png的可以省略
    background.image = [UIImage imageNamed:@"back2.jpg"];

    [self.window addSubview:background];

    //2.创建小鸟
    UIImage *image = [UIImage imageNamed:@"DOVE 1"];

    UIImageView *bird = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, image.size.width, image.size.height)];
    bird.tag = 100;
    [self.window addSubview:bird];

    //动画:实际上就是在一定时间内播放指定张数的图片
    //每秒最少要播放24帧(一张图片)

    //保存小鸟的图片
    NSMutableArray *mutarray = [NSMutableArray array];

    //全部放到数组里面
    for (int i = 1; i<=18; i++) {
        UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:@"DOVE %i",i]];

        [mutarray addObject:image1];
    }

    [bird setAnimationImages:mutarray];//在一定的时间里面要播放的图片张数
    [bird setAnimationDuration:0.5];//动画持续的时间
    [bird setAnimationRepeatCount:0];//0代表无限制

    [bird startAnimating];//开始动画

    [NSTimer scheduledTimerWithTimeInterval:1.0/60. target:self selector:@selector(birdMove:) userInfo:nil repeats:YES];

    //设置玩家
    UIImage *image2 = [UIImage imageNamed:@"player"];
    UIImageView *player = [[UIImageView alloc]initWithImage:image2];
    player.frame = CGRectMake(100, 350, image2.size.width, image2.size.height);

    player.center = CGPointMake(160, 350);
    player.tag = 101;

    //background要进行手动设置,才能交互
    background.userInteractionEnabled = YES;
    [background addSubview:player];
    //[self.window addSubview:player];

    //添加移动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panPlayer:)];

    //将手势添加到目标对象上面

    //UIimageView本身不可进行交互,要手动设置
    player.userInteractionEnabled = YES;
    //self.window.userInteractionEnabled = YES;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
//滑动手指要执行的方法
-(void)panPlayer:(UIPanGestureRecognizer *)pan{
    //得到player对象
    UIImageView *player = (UIImageView *)[self.window viewWithTag:101];
    //InView:目标所在的父视图
    CGPoint point = [pan locationInView:self.window];

    //让player的中心点随着手势改变
    player.center = point;

}

//让鸟移动
-(void)birdMove:(NSTimer *)timer{
    //addSubView:可以使父视图拥有子视图的所有权
    //从window里面取出bird这个子视图
    UIImageView *imageView = (UIImageView *)[self.window viewWithTag:100];

    CGRect frame = imageView.frame;
    //是鸟的x坐标增加1
    frame.origin.x += 1;

    //当鸟飞到最右边的时候,重新从左边开始飞
    if (frame.origin.x >= 320) {
        frame.origin.x = -frame.size.width;
    }

    imageView.frame = frame;
}

- (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