大綱:
創(chuàng)建 UIImageView 和 UIImage (兩者關(guān)系贸铜,構(gòu)造方法構(gòu)建,類方法構(gòu)建)
基本設(shè)置(位置聂受,大小蒿秦,背景顏色)
圖片填充模式(拉伸蛋济,居中......)
圖片的平移與縮放
動畫(創(chuàng)建動畫棍鳖,動畫設(shè)置,動畫播放/停止)
手勢(創(chuàng)建手勢碗旅,設(shè)置手勢鹊杖,啟用手勢,獲取手勢所在的View)
開發(fā)小技巧
一扛芽、創(chuàng)建UIImageView 和 UIImage
//創(chuàng)建UIImageView和UIimage
//兩者關(guān)系類似于NSString和UILabel
//UIImage繼承自NSObject骂蓖,是一個對象,不是視圖
//UIImageView繼承自UIView川尖,是一個視圖
//使用構(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;
四馍悟、圖片的平移與縮放
//移動
//第一個參數(shù):原來的Transform
//第二個參數(shù):X軸平移距離
//第三個參數(shù):Y軸平移距離
CGAffineTransform old = imageView.transform;
CGAffineTransform new = CGAffineTransformTranslate(old, 10, 0);
imageView.transform = new;
//縮放
//第一個參數(shù):原來的Transform
//第二個參數(shù):X軸按比例縮放
//第三個參數(shù):Y軸按比例縮放
CGAffineTransform old = imageView.transform;
CGAffineTransform new = CGAffineTransformScale(old, 0.8, 0.8);
imageView.transform = new;
五畔濒、動畫(創(chuàng)建動畫,動畫設(shè)置锣咒,動畫播放/停止)
//創(chuàng)建動畫過程
//獲取動畫侵状,先創(chuàng)建一個UIImageView的對象(不然一開始是沒有任何圖像在屏幕上)
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 50, 50)];
_imageView.image = [UIImage imageNamed:@"DOVE 1.png"];
//設(shè)置動畫的背景
UIImageView * backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
backImage.image = [UIImage imageNamed:@"back2.jpg"];
//創(chuàng)建一個數(shù)組存儲圖片赞弥,數(shù)組的元素是UIImage的對象
NSMutableArray * arr = [[NSMutableArray alloc]init];
for (int i = 1; i < 19; i++) {
//使用UIImage的對象獲取圖片
UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"DOVE %d.png",i]];
[arr addObject:image];
}
//設(shè)置動畫每一幀的圖片
_imageView.animationImages = arr;
//設(shè)置動畫的播放間隔
_imageView.animationDuration = 1/24(設(shè)置多少幀每秒);
//設(shè)置重復(fù)次數(shù)(0為不停重復(fù))
_imageView.animationRepeatCount = 0;
//設(shè)置動畫開始
[_imageView startAnimating];
//設(shè)置動畫停止
[_imageView stopAnimating];
六、手勢(創(chuàng)建手勢趣兄,設(shè)置手勢绽左,啟用手勢,獲取手勢所在的View——詳見 Day12 的筆記)
//創(chuàng)建手勢
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
//觸發(fā)該手勢需要 點擊 的次數(shù)
tap.numberOfTapsRequired = 1;
//觸發(fā)該手勢需要 手指 的次數(shù)
tap.numberOfTouchesRequired = 1;
//給手勢設(shè)置執(zhí)行的方法
[tap addTarget:self action:@selector(test:)];
//在View上面加入手勢
[view3 addGestureRecognizer:tap];
//獲取手勢所在的View
UIView * tmpView = tap.view;
七艇潭、開發(fā)小技巧
//View(顏色為clear)+手勢的應(yīng)用(毆打Tomcat)
//動畫可以加上計時器一起運用(結(jié)合飛行標簽)
//圖片的平移和縮放結(jié)合計時器的運用(淘寶購物車效果)
//好好利用屬性拼窥,省去傳參麻煩