8、UIImageView-OC+UI

如果要将图片放到程序中要用到UIImageView


UIImageView 和 UIImage:


UIImage:

方法一:[小图片,重复利用]

方法二:[大图片,不重复]


UIImageView 常用属性:

UIImageView 动画:


//
//  ccyAppDelegate.m
//  UIImageViewDemo
//
//  Created by ccy on 13-12-26.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import "ccyAppDelegate.h"

@implementation ccyAppDelegate
{
    UIImageView * _imageView1;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    UIImage * image = [UIImage imageNamed:@"57x57.png"];
    //UIImage * image = [UIImage imageNamed:@"test.jpg"];
    UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
    //设置显示区域大小 == 图片大小
    //imageView.frame = CGRectMake(10, 20, image.size.width , image.size.height);
    //设置固定显示区域大小
    imageView.frame = CGRectMake(10, 30, 300, 100);
    
    imageView.backgroundColor = [UIColor redColor];
    //设置内容模式 contentMode 是UIView属性
    /*
     UIViewContentModeCenter - 图片不被拉伸,居中显示
     
     UIViewContentModeScaleToFill, - 将图片拉伸(比例可能改变),充满整个屏幕
     
     UIViewContentModeScaleAspectFit, - 将图片拉伸(比例不变),小的一边充满ImageView,大的一边可能超出ImageView的frame
     // contents scaled to fit with fixed aspect. remainder is transparent
     
     UIViewContentModeScaleAspectFill, - 将图片拉伸(比例不变),大的一边充满ImageView,小的一边可能超出ImageView的frame    
     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
     
     UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
     每次改变frame都会调用这个方法
     */
    imageView.contentMode = UIViewContentModeRedraw;
    
    [self.window addSubview:imageView];
    //[imageView release];
    
    /*图片动画*/
    NSMutableArray * images = [[NSMutableArray alloc] init];
    for (int i=1; i<=9; i++) {
        
        UIImage * image1 = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        [images addObject:image1];
    }
    _imageView1 = [[UIImageView alloc] init];
    _imageView1.frame = CGRectMake(10, 150, 300, 100);
    //设置动画数组 (图片数组)
    _imageView1.animationImages = images;
    //设置动画周期 (全部播放需要的时间)单位 s
    _imageView1.animationDuration = 1;
    //设置动画播放次数 (缺省:无限播放)
    //_imageView1.animationRepeatCount = 5;
    
    [self.window addSubview:_imageView1];
    
    //[_imageView1 startAnimating];
    //动画结束按钮
    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(5, 260, 90, 20);
    btn1.tag = 1;
    [btn1 setTitle:@"停止动画" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn1];
    
    //动画检测状态按钮
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(100, 260, 90, 20);
    btn2.tag = 2;
    [btn2 setTitle:@"动画状态" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn2];
    
    //动画启动按钮
    UIButton * btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn3.frame = CGRectMake(195, 260, 90, 20);
    btn3.tag = 3;
    [btn3 setTitle:@"开启动画" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn3];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)btnclick:(id)arg
{
    UIButton * btn = (UIButton *)arg;
    switch (btn.tag) {
        case 1:
            //结束
            [_imageView1 stopAnimating];
            break;
         case 2:
            //检测
            if([_imageView1 isAnimating])
                NSLog(@"动画正在运行");
            else
                NSLog(@"动画停止运行");
            break;
        case 3:
            //启动
            [_imageView1 startAnimating];
            break;
        default:
            break;
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值