日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios之UIImageView

發(fā)布時間:2025/4/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios之UIImageView 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

UIImageView,顧名思義,是用來放置圖片的。使用Interface Builder設(shè)計界面時,當然可以直接將控件拖進去并設(shè)置相關(guān)屬性,這就不說了,這里講的是用代碼。

1、創(chuàng)建一個UIImageView:

創(chuàng)建一個UIImageView對象有五種方法:

UIImageView *imageView1 = [[UIImageView alloc] init]; UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)]; UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)]; UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)]; UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];

?

?

比較常用的是前邊三個。至于第四個,當這個ImageView的highlighted屬性是YES時,顯示的就是參數(shù)highlightedImage,一般情況下顯示的是第一個參數(shù)UIImage。

2、frame與bounds屬性:

上述創(chuàng)建一個UIImageView的方法中,第二個方法是在創(chuàng)建時就設(shè)定位置和大小。

當之后想改變位置時,可以重新設(shè)定frame屬性:

imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);

?

?

注意到UIImageView還有一個bounds屬性

imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);

?

?

那么這個屬性跟frame有什么區(qū)別呢?

我的理解是,frame設(shè)置其位置和大小,而bounds只能設(shè)置其大小,其參數(shù)中的x、y不起作用即便是之前沒有設(shè)定frame屬性,控件最終的位置也不是bounds所設(shè)定的參數(shù)。bounds實現(xiàn)的是將UIImageView控件以原來的中心為中心進行縮放。例如有如下代碼:

imageView.frame = CGRectMake(0, 0, 320, 460); imageView.bounds = CGRectMake(100, 100, 160, 230);

?

?

執(zhí)行之后,這個imageView的位置和大小是(80, 115, 160, 230)。

3、contentMode屬性:

這個屬性是用來設(shè)置圖片的顯示方式,如居中、居右,是否縮放等,有以下幾個常量可供設(shè)定:

UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft?UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight

?

?

注意以上幾個常量,凡是沒有帶Scale的,當圖片尺寸超過 ImageView尺寸時,只有部分顯示在ImageView中。UIViewContentModeScaleToFill屬性會導(dǎo)致圖片變形。UIViewContentModeScaleAspectFit會保證圖片比例不變,而且全部顯示在ImageView中,這意味著ImageView會有部分空白。UIViewContentModeScaleAspectFill也會證圖片比例不變,但是是填充整個ImageView的,可能只有部分圖片顯示出來。

前三個效果如下圖:

??????

???UIViewContentModeScaleToFill ???UIViewContentModeScaleAspectFit ?UIViewContentModeScaleAspectFill

4、更改位置

更改一個UIImageView的位置,可以

4.1 直接修改其frame屬性

4.2 修改其center屬性:

imageView.center = CGPointMake(CGFloat x, CGFloat y);

?

?

center屬性指的就是這個ImageView的中間點。

4.3 使用transform屬性

imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);

?

?

其中dx與dy表示想要往x或者y方向移動多少,而不是移動到多少。

5、旋轉(zhuǎn)圖像

imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);

?

?

要注意它是按照順時針方向旋轉(zhuǎn)的,而且旋轉(zhuǎn)中心是原始ImageView的中心,也就是center屬性表示的位置。

這個方法的參數(shù)angle的單位是弧度,而不是我們最常用的度數(shù),所以可以寫一個宏定義:

?

#define degreesToRadians(x) (M_PI*(x)/180.0)

?

用于將度數(shù)轉(zhuǎn)化成弧度。下圖是旋轉(zhuǎn)45度的情況:

?

???

6、縮放圖像

還是使用transform屬性:

imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);

?

?

其中,CGFloat scale_w與CGFloat scale_h分別表示將原來的寬度和高度縮放到多少倍,下圖是縮放到原來的0.6倍的示意圖:

???

7、播放一系列圖片

?

imageView.animationImages = imagesArray; // 設(shè)定所有的圖片在多少秒內(nèi)播放完畢 imageView.animationDuration = [imagesArray count]; // 不重復(fù)播放多少遍,0表示無數(shù)遍 imageView.animationRepeatCount = 0; // 開始播放 [imageView startAnimating];

?

其中,imagesArray是一些列圖片的數(shù)組。如下圖:

?

??????

8、為圖片添加單擊事件:

imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:singleTap];

?

?

一定要先將userInteractionEnabled置為YES,這樣才能響應(yīng)單擊事件。

9、其他設(shè)置

imageView.hidden = YES或者NO;????// 隱藏或者顯示圖片 imageView.alpha = (CGFloat) al;????// 設(shè)置透明度 imageView.highlightedImage = (UIImage *)hightlightedImage; // 設(shè)置高亮時顯示的圖片 imageView.image = (UIImage *)image; // 設(shè)置正常顯示的圖片 [imageView sizeToFit];????// 將圖片尺寸調(diào)整為與內(nèi)容圖片相同

轉(zhuǎn)載于:https://www.cnblogs.com/yulang314/p/3550349.html

總結(jié)

以上是生活随笔為你收集整理的ios之UIImageView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。