IOS界面开发之UIImageView

这里学习下UIImageView控件,添加了一个按钮来启动动画和显示UIImage与载体UIImageView的使用。

//
//  ImageViewController.m
//  UIViewDemo
//
//  Created by dcr on 2016/12/16.
//  Copyright © 2016年 All rights reserved.
//

#import "ImageViewController.h"

@interface ImageViewController ()

@end

@implementation ImageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
    UIButton *btnImageView = [UIButton buttonWithType:UIButtonTypeSystem];
    btnImageView.frame = CGRectMake(0, rect.size.height, 100, 30);
    btnImageView.backgroundColor = [UIColor whiteColor];
    btnImageView.titleLabel.font = [UIFont systemFontOfSize:16];
    [btnImageView setTitle:@"返回" forState:UIControlStateNormal];
    [btnImageView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnImageView addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnImageView];
    
    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];
    start.frame = CGRectMake(0, rect.size.height + 40, 100, 30);
    start.backgroundColor = [UIColor whiteColor];
    start.titleLabel.font = [UIFont systemFontOfSize:16];
    [start setTitle:@"开始动画" forState:UIControlStateNormal];
    [start setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [start addTarget:self action:@selector(btnStartAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:start];

#if 0
    //NSString *path = [[NSBundle mainBundle] resourcePath];
    //NSString *imgPath = [NSString stringWithFormat:@"%@/287.png", path];
    //从文件路径加载
    //UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgPath];
    //用这种方式加载图片会被加入缓存中,对内存会有压力,根据情况使用
    UIImage *img = [UIImage imageNamed:@"287"];
    //载体
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    //原图大小
    NSLog(@"w = %f, h = %f", img.size.width, img.size.height);
    //图片显示大小是由载体控制的
    imgView.frame = CGRectMake(50, rect.size.height + 80, 100, 100);
    imgView.backgroundColor = [UIColor yellowColor];
    //内容模式
    /**
     默认效果UIViewContentModeScaleToFill  拉伸充满整个载体
     UIViewContentModeScaleAspectFill  拉伸不改变比例,充满最大的一边
     UIViewContentModeScaleAspectFit   拉伸不改变比例,充满最小的一边
     
     */
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imgView];
#endif
}

- (void)btnBack{
    [self dismissViewControllerAnimated:true completion:^{
        NSLog(@"ImageViewController back completion");
    }];
}

- (void)btnStartAnimation{
    //UIImageView动画 -- 播放序列图
    NSArray *arr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"337"],
                    [UIImage imageNamed:@"338"],
                    [UIImage imageNamed:@"339"],
                    [UIImage imageNamed:@"340"],
                    [UIImage imageNamed:@"341"],
                    [UIImage imageNamed:@"342"], nil];
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.frame = CGRectMake(0, 100, 320, 200);
    //imgView.frame = self.view.frame;
    imgView.backgroundColor = [UIColor redColor];
    imgView.contentMode = UIViewContentModeCenter;
    
    [self.view addSubview:imgView];

    //设置动画数组
    imgView.animationImages = arr;
    //设置播放周期
    imgView.animationDuration = 3;
    //设置动画次数,如果设置为0则无限循环
    imgView.animationRepeatCount = 100;
    [imgView startAnimating];
    for(UIImage *img in arr){
        NSLog(@"w = %f, h = %f", img.size.width, img.size.height);
    }
    NSLog(@"is animation = %d", [imgView isAnimating]);

}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
完整示例代码下载: http://download.csdn.net/detail/deng0zhaotai/9715127

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值