zoukankan      html  css  js  c++  java
  • UIImageView的使用

    1. 常见的属性
     // 1.创建UIImageView的对象
        UIImageView *imageView = [[UIImageView alloc] init];
        
        // 2.设置frame
        imageView.frame = CGRectMake(100, 100, 175, 175);
        
        // 3.设置背景颜色
        imageView.backgroundColor = [UIColor yellowColor];
        
        // 4.设置显示的图片
        imageView.image = [UIImage imageNamed:@"1"];
        
        // 5.设置内容模式
        /*
         UIViewContentModeRedraw, 重新绘制 drawRect
         
         // 带Scale比例--图片之后可能会被缩放
         UIViewContentModeScaleToFill, // 默认情况 : 压缩或者拉伸图片,让图片可以填充整个控件
         
         UIViewContentModeScaleAspectFit, // 宽度比例不变 : 图片可以被拉伸也可与被压缩,但是保持宽高比.Fit:适应,一部分填充
         UIViewContentModeScaleAspectFill,  // 宽度比例不变 : 图片可以被拉伸也可与被压缩,但是保持宽高比.Fill:填充
         
         // 图片不会被拉伸和压缩
         UIViewContentModeCenter,
         UIViewContentModeTop,
         UIViewContentModeBottom,
         UIViewContentModeLeft,
         UIViewContentModeRight,
         
         UIViewContentModeTopLeft,
         UIViewContentModeTopRight,
         UIViewContentModeBottomLeft,
         UIViewContentModeBottomRight,
         */
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // 将对象添加到控制器的View中
        [self.view addSubview:imageView];
        
        // 设置超出控件的部分剪切掉
        imageView.clipsToBounds = YES;

    2.添加毛玻璃效果

     // 1.创建UIImageView作为背景
        UIImageView *bgImageView = [[UIImageView alloc] init];
        
        // 2.设置frame
        bgImageView.frame = self.view.bounds;
        // bgImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        
        // 3.设置背景颜色
        bgImageView.backgroundColor = [UIColor yellowColor];
        
        // 4.设置显示的背景图片
        bgImageView.image = [UIImage imageNamed:@"1"];
        
        // 5.设置内容模式
        bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // 6.添加毛玻璃效果
        // 6.1.创建UIToolbar对象
        UIToolbar *toolbar = [[UIToolbar alloc] init];
        
        // 6.2.设置toolbar的样式
        toolbar.barStyle = UIBarStyleBlack;
        
        // 6.2.设置frame
        toolbar.frame = bgImageView.bounds;
        
        // 6.3.将toolBar添加到bgImageView中
        [bgImageView addSubview:toolbar];
        
        // 添加到控制器的View
        [self.view addSubview:bgImageView];

    3.UIImageView 的frame的设置的四种方式

    // 1.最基本的设置frame的方式
        
        // 1.1.创建UIImageView对象
        UIImageView *imageView1 = [[UIImageView alloc] init];
        
        // 1.2.设置frame
        imageView1.frame = CGRectMake(50, 50, 267, 400);
        
        // 1.3.设置图片
        imageView1.image = [UIImage imageNamed:@"1"];
        
        
        // 2.根据图片的大小设置frame的方式(掌握)
        
        // 2.1.创建UIimageView对象
        UIImageView *imageView2 = [[UIImageView alloc] init];
        
        // 2.2.加载图片
        UIImage *image2 = [UIImage imageNamed:@"1"];
        
        // 2.3.设置frame
        imageView2.frame = CGRectMake(50, 50, image2.size.width, image2.size.height);
        
        // 2.4.设置图片
        imageView2.image = image2;
        
        
        // 3.创建UIImageView对象时,直接设置frame
        
        // 3.1.加载图片
        UIImage *image = [UIImage imageNamed:@"1"];
        
        // 3.2.创建UIImageView的对象,同时初始化frame
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, image.size.width, image.size.height)];
        
        // 3.3.设置图片
        imageView.image = image;
        
        
        // 4.创建UIImageView对象时,直接设置图片(掌握)
        // 4.1.创建UIImageView对象,并且设置要显示的图片
        // 注意:如果初始化时设置图片,那么图片的尺寸就是imageView的尺寸
        UIImageView *imageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
        
        // 4.2.改变imageView的位置
        imageView4.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
        
        // 超出imageView的部分剪切
        imageView4.clipsToBounds = YES;
        
        // 添加到控制器的View中
        [self.view addSubview:imageView];

    4.UIImageView的帧动画

    1.将所有的要播放的图片添加进一个数组保存,然后复制给ImageView的animationImages属性

     // 1.加载所以的图片
        NSMutableArray *images = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            // 1.1.获取图片的名称
            NSString *imageName = [NSString stringWithFormat:@"%d", i + 1];
            
            // 1.2.加载图片
            UIImage *image = [UIImage imageNamed:imageName];
            
            // 1.3.将image对象放入数组中
            [images addObject:image];
        }
        
        // 2.设置动画播放的图片
        self.imageView.animationImages = images;

    5.通过ImageView的属性设置开启动画播放

    // 3.设置动画播放的次数
        self.imageView.animationRepeatCount = 0;
        
        // 4.设置动画执行的时间
        // 默认情况下:一秒钟30帧,一秒播放30张图片.一张图片的时间:1/30.0=0.033
        // 当设置成1.0,播放一轮图片所使用的时间. 1/20.0=0.05
        self.imageView.animationDuration = 0.5;
        
        // 6.查看是否正在动画
    //     self.imageView.isAnimating
    }
    
    #pragma mark - 对动画的操作
    - (IBAction)startAnimating {
        [self.imageView startAnimating];
    }
    
    - (IBAction)stopAnimating {
        [self.imageView stopAnimating];
    }

    6.拳皇动画

    1.加载图片

    #pragma mark - 执行动画
    #pragma mark 站立
    - (IBAction)stand {
        // 1.加载图片资源
        /*
        NSMutableArray *standImages = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            NSString *imageName = [NSString stringWithFormat:@"stand_%d", i + 1];
            UIImage *image = [UIImage imageNamed:imageName];
            [standImages addObject:image];
        }
         */
        
        // 2.设置动画的图片
        self.imageView.animationImages = self.standImages;
        
        // 3.设置动画的执行次数
        self.imageView.animationRepeatCount = 0;
        
        // 4.开始动画
        [self.imageView startAnimating];
    }
    
    #pragma mark 小招
    - (IBAction)smallZhao {
        // 1.加载图片资源
        /*
        NSMutableArray *smallZhaoImages = [NSMutableArray array];
        for (int i = 0; i < 21; i++) {
            NSString *imageName = [NSString stringWithFormat:@"xiaozhao1_%d", i + 1];
            UIImage *image = [UIImage imageNamed:imageName];
            [smallZhaoImages addObject:image];
        }
         */
        
        // 2.设置动画的图片
        self.imageView.animationImages = self.smallZhaoImages;
        
        // 3.设置动画的执行次数
        self.imageView.animationRepeatCount = 1;
        
        // 4.开始动画
        [self.imageView startAnimating];
    }
    
    #pragma mark 大招
    - (IBAction)bigZhao {
        // 1.加载图片资源
        /*
        NSMutableArray *bigZhaoImages = [NSMutableArray array];
        for (int i = 0; i < 87; i++) {
            NSString *imageName = [NSString stringWithFormat:@"dazhao_%d", i + 1];
            UIImage *image = [UIImage imageNamed:imageName];
            [bigZhaoImages addObject:image];
        }
         */
        
        // 2.设置动画的图片
        self.imageView.animationImages = self.bigZhaoImages;
        
        // 3.设置动画的执行次数
        self.imageView.animationRepeatCount = 1;
        
        // 4.开始动画
        [self.imageView startAnimating];

    2.拳皇动画(放招后站立)

    // 所以的图片资源
    @property (nonatomic, strong) NSArray *standImages;
    @property (nonatomic, strong) NSArray *smallZhaoImages;  增加属性保存照片
    @property (nonatomic, strong) NSArray *bigZhaoImages;
    
    @end
    
    @implementation ViewController
    
    /*
     注意:
        通过imageName:来加载的图片,指向它的强引用销毁时,图片不会随着一起销毁
        通过imageWithContentOfFile:加载的图片,指向它的强引用销毁时,图片会随着一起销毁
     */
    
    #pragma mark - 初始化数据
    // 初始化一些控件
    // 初始化一些数据
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 1.加载站立所以的图片
        self.standImages = [self loadImagesWithImagePrefix:@"stand" count:10];
        
        // 2.加载小招的所以图片
        self.smallZhaoImages = [self loadImagesWithImagePrefix:@"xiaozhao1" count:21];
        
        // 3.加载大招的所以图片
        self.bigZhaoImages = [self loadImagesWithImagePrefix:@"dazhao" count:87];
        
        // 4.让人物处于站立状态
        [self stand];
    }
    
    // 抽取加载图片的方法 将相同操作抽取成一个方法封装,将不同的变量设置成参数,传递进来
    - (NSArray *)loadImagesWithImagePrefix:(NSString *)imagePrefix count:(NSInteger)count
    {
        NSMutableArray *images = [NSMutableArray array];
        for (int i = 0; i < count; i++) {
            NSString *imageName = [NSString stringWithFormat:@"%@_%d", imagePrefix, i + 1];
            // UIImage *image = [UIImage imageNamed:imageName];
            NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
            UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
            [images addObject:image];
        }
        
        return images;
    }
    
    #pragma mark - 执行动画
    #pragma mark 站立
    - (IBAction)stand {
        // 1.设置动画的图片
        self.imageView.animationImages = self.standImages;
        
        // 2.设置动画的执行次数
        self.imageView.animationRepeatCount = 0;
        
        // 3.开始动画
        [self.imageView startAnimating];
    }
    
    #pragma mark 小招
    - (IBAction)smallZhao {
        /*
        // 1.设置动画的图片
        self.imageView.animationImages = self.smallZhaoImages;
        
        // 2.设置动画的执行次数
        self.imageView.animationRepeatCount = 1;
        
        // 3.开始动画
        [self.imageView startAnimating];
        
        // 4.执行站立动画
        NSTimeInterval delayTime = 1 / 30.0 * self.smallZhaoImages.count - 0.08;
        [self performSelector:@selector(stand) withObject:nil afterDelay:delayTime];
         */设置动画执行的时间,在动画执行完毕后执行后面的语句,延迟执行的应用
        [self playZhaoWithImages:self.smallZhaoImages];
    }
    
    #pragma mark 大招
    - (IBAction)bigZhao {
        /*
        // 1.设置动画的图片
        self.imageView.animationImages = self.bigZhaoImages;
        
        // 2.设置动画的执行次数
        self.imageView.animationRepeatCount = 1;
        
        // 3.开始动画
        [self.imageView startAnimating];
        
        // 4.执行站立动画
        NSTimeInterval delayTime = 1 / 30.0 * self.bigZhaoImages.count - 0.08;
        [self performSelector:@selector(stand) withObject:nil afterDelay:delayTime];
         */
        [self playZhaoWithImages:self.bigZhaoImages];
    }
    
    - (void)playZhaoWithImages:(NSArray *)images
    {
        // 1.设置动画的图片
        self.imageView.animationImages = images;
        
        // 2.设置动画的执行次数
        self.imageView.animationRepeatCount = 1;
        
        // 3.开始动画
        [self.imageView startAnimating];
        
        // 4.执行站立动画
        NSTimeInterval delayTime = 1 / 30.0 * images.count - 0.08;
        [self performSelector:@selector(stand) withObject:nil afterDelay:delayTime];
    }
    
    #pragma mark - 游戏结束
    - (IBAction)gameOver {
        self.standImages = nil;
        self.smallZhaoImages = nil;
        self.bigZhaoImages = nil;
       ARC内存管理
        self.imageView.animationImages = nil;
    }

    3.IOS中播放声音

    import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>拷贝头文件
    
    @interface ViewController ()
    
    // 播放器
    @property (nonatomic, strong) AVPlayer *player;保存播放器,离开创建的方法不会呗销毁
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        /*
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1201111234.mp3" ofType:nil];
        NSURL *url = [NSURL fileURLWithPath:filePath];
         */
        
        // 1.获取资源的URL 从MainBun加载音频资源
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"1201111234.mp3" withExtension:nil];
        
        // 2.创建AVPlayer
        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
        AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
        
        self.player = player;
       
    }
    
    - (IBAction)play {
        [self.player play];播放音频方法
    }
    
    - (IBAction)nextMusic {
        // 替换item
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"120125029.mp3" withExtension:nil];
        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
        [self.player replaceCurrentItemWithPlayerItem:item];
        
        // 播放音效
        [self.player play];
    }

    4.播放拳皇动画的声音

    - (void)playZhaoWithImages:(NSArray *)images withSoundName:(NSString *)soundName
    {
        // 1.设置动画的图片
        self.imageView.animationImages = images;
        
        // 2.设置动画的执行次数
        self.imageView.animationRepeatCount = 1;
        
        // 3.开始动画
        [self.imageView startAnimating];
        
        // 4.执行站立动画
        NSTimeInterval delayTime = 1 / 30.0 * images.count - 0.08;
        [self performSelector:@selector(stand) withObject:nil afterDelay:delayTime];
        
        // 5.播放音效
        NSURL *url = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"mp3"];
        AVPlayerItem *resplaceItem = [[AVPlayerItem alloc] initWithURL:url];
        [self.player replaceCurrentItemWithPlayerItem:resplaceItem];
        [self.player play];
        
        // 调节声音的速率  匹配声音跟动画同步
        self.player.rate = 1.5;
    }
  • 相关阅读:
    微信小程序非域名测试环境,手机预览不正常,模拟器和真机调试正常解决办法
    vue 建项目及初步开发遇到的问题
    开发&测试
    架构&项目
    运维
    读书&源码
    ArrayList
    jdk集合知识点整理
    jdk集合关系图
    jvm知识点整理
  • 原文地址:https://www.cnblogs.com/mshong1616/p/UIImageView.html
Copyright © 2011-2022 走看看