6--UIImageView 和 UIImage

大綱:

創(chuàng)建 UIImageView 和 UIImage (兩者關(guān)系,構(gòu)造方法構(gòu)建,類方法構(gòu)建)

基本設(shè)置(位置,大小,背景顏色)

圖片填充模式(拉伸,居中......)

圖片的平移與縮放

動(dòng)畫(創(chuàng)建動(dòng)畫,動(dòng)畫設(shè)置,動(dòng)畫播放/停止)

手勢(shì)(創(chuàng)建手勢(shì),設(shè)置手勢(shì),啟用手勢(shì),獲取手勢(shì)所在的View)

開發(fā)小技巧

一、創(chuàng)建UIImageView 和 UIImage

//創(chuàng)建UIImageView和UIimage 
//兩者關(guān)系類似于NSString和UILabel
//UIImage繼承自NSObject,是一個(gè)對(duì)象,不是視圖
//UIImageView繼承自UIView,是一個(gè)視圖
//使用構(gòu)造方法構(gòu)建
    UIImage * image = [UIImage imageNamed:@"mouse.png"];
    UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
//使用類方法構(gòu)建
    UIImageView * imageView1 = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"mouse.png"];

二、基本設(shè)置(位置,大小,背景顏色)

//設(shè)置位置和大小
    imageView.frame = CGRectMake(10, 20, 150, 50);
//設(shè)置背景顏色
    [imageView setBackgroundColor:[UIColor redColor]];

三、圖片填充模式(拉伸,居中......)

//UIViewContentModeScaleToFill,         拉伸
//UIViewContentModeScaleAspectFit,      按照寬高的最小者的比例縮放
//UIViewContentModeScaleAspectFill,     按照寬高的最大者的比例縮放
//UIViewContentModeRedraw,
//UIViewContentModeCenter,
//UIViewContentModeTop,
//UIViewContentModeBottom,
//UIViewContentModeLeft,
//UIViewContentModeRight,
//UIViewContentModeTopLeft,
//UIViewContentModeTopRight,
//UIViewContentModeBottomLeft,
//UIViewContentModeBottomRight,
    imageView.contentMode = UIViewContentModeScaleToFill;

四、圖片的平移與縮放

//移動(dòng)
//第一個(gè)參數(shù):原來(lái)的Transform
//第二個(gè)參數(shù):X軸平移距離
//第三個(gè)參數(shù):Y軸平移距離
        CGAffineTransform old = imageView.transform;
        CGAffineTransform new = CGAffineTransformTranslate(old, 10, 0);
        imageView.transform = new;
//縮放
//第一個(gè)參數(shù):原來(lái)的Transform
//第二個(gè)參數(shù):X軸按比例縮放
//第三個(gè)參數(shù):Y軸按比例縮放
        CGAffineTransform old = imageView.transform;
        CGAffineTransform new = CGAffineTransformScale(old, 0.8, 0.8);
        imageView.transform = new;

五、動(dòng)畫(創(chuàng)建動(dòng)畫,動(dòng)畫設(shè)置,動(dòng)畫播放/停止)

//創(chuàng)建動(dòng)畫過(guò)程
//獲取動(dòng)畫,先創(chuàng)建一個(gè)UIImageView的對(duì)象(不然一開始是沒有任何圖像在屏幕上)
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 50, 50)];
    _imageView.image = [UIImage imageNamed:@"DOVE 1.png"];
//設(shè)置動(dòng)畫的背景
    UIImageView * backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    backImage.image = [UIImage imageNamed:@"back2.jpg"];
//創(chuàng)建一個(gè)數(shù)組存儲(chǔ)圖片,數(shù)組的元素是UIImage的對(duì)象
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    for (int i = 1; i < 19; i++) {
//使用UIImage的對(duì)象獲取圖片
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"DOVE %d.png",i]];
        [arr addObject:image];
    }
//設(shè)置動(dòng)畫每一幀的圖片
    _imageView.animationImages = arr;
//設(shè)置動(dòng)畫的播放間隔
    _imageView.animationDuration = 1/24(設(shè)置多少幀每秒);
//設(shè)置重復(fù)次數(shù)(0為不停重復(fù))
    _imageView.animationRepeatCount = 0;
//設(shè)置動(dòng)畫開始
    [_imageView startAnimating];
//設(shè)置動(dòng)畫停止
    [_imageView stopAnimating];

六、手勢(shì)(創(chuàng)建手勢(shì),設(shè)置手勢(shì),啟用手勢(shì),獲取手勢(shì)所在的View——詳見 Day12 的筆記)

//創(chuàng)建手勢(shì)
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
//觸發(fā)該手勢(shì)需要 點(diǎn)擊 的次數(shù)
    tap.numberOfTapsRequired = 1;
//觸發(fā)該手勢(shì)需要 手指 的次數(shù)
    tap.numberOfTouchesRequired = 1;
//給手勢(shì)設(shè)置執(zhí)行的方法
    [tap addTarget:self action:@selector(test:)];
//在View上面加入手勢(shì)
    [view3 addGestureRecognizer:tap];
//獲取手勢(shì)所在的View
    UIView * tmpView = tap.view;

七、開發(fā)小技巧

//View(顏色為clear)+手勢(shì)的應(yīng)用(毆打Tomcat)
//動(dòng)畫可以加上計(jì)時(shí)器一起運(yùn)用(結(jié)合飛行標(biāo)簽)
//圖片的平移和縮放結(jié)合計(jì)時(shí)器的運(yùn)用(淘寶購(gòu)物車效果)
//好好利用屬性,省去傳參麻煩
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,405評(píng)論 4 61
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,694評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,270評(píng)論 5 13
  • 2016年12月31日到2017年1月2日這三天我在成都度過(guò),這是我第一次去成都旅行,也是我新年的第一次長(zhǎng)途旅行。...
    Miss童閱讀 5,493評(píng)論 25 56
  • 今天想談?wù)勎蚁壬?1,小鮮肉&老臘肉 沒結(jié)婚之前,我家先生號(hào)稱自己是小鮮肉,用他的話說(shuō)評(píng)價(jià)自己是年輕、帥氣又健碩...
    悠然榛子閱讀 495評(píng)論 4 2

友情鏈接更多精彩內(nèi)容