UIImageView 再常見(jiàn)不過(guò)的東西了。毁菱。舍扰。
//創(chuàng)建
//創(chuàng)建的時(shí)候直接設(shè)置圖片
let imageView = UIImageView(image:UIImage(named:"girl"))
//先創(chuàng)建出對(duì)象再設(shè)置圖片
let imageView1 = UIImageView()
imageView1.image = UIImage(named:"girl")
//圖片獲取
1砖茸、從文件目錄中獲取圖片
let path = Bundle.main.path(forResource:"girl", ofType: "png")
let newImage = UIImage(contentsOfFile: path!)
2、網(wǎng)絡(luò)地址獲取圖片
let url = URL(string:"http://image.cnpp.cn/upload/images/20160905/09380421552_400x300.jpg")
let data = try! Data(contentsOf: url!)
let smallImage = UIImage(data: data)
//imageView1.image = smallImage
let imageView1 = UIImageView(image:smallImage)
//圖片顯示填充樣式
imageView1.contentMode = UIViewContentMode.scaleAspectFit
對(duì)于UIImageView的圖片填充樣式有多種:
/*
public enum UIViewContentMode : Int {
case scaleToFill
case scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
case scaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
case redraw // redraw on bounds change (calls -setNeedsDisplay)
case center // contents remain same size. positioned adjusted.
case top
case bottom
case left
case right
case topLeft
case topRight
case bottomLeft
case bottomRight
}
*/
//分組圖片輪展
UIImageView中有設(shè)置多張圖片一塊展示的功能碉碉,類(lèi)似于幻燈片的自動(dòng)播放柴钻。其實(shí)現(xiàn)過(guò)程如下:
//設(shè)置圖片數(shù)組
imageView1.animationImages = [UIImage(named:"2")!,UIImage(named:"3")!]
//所有圖片展示完一遍的總時(shí)長(zhǎng)
imageView1.animationDuration = 2
//開(kāi)始
imageView1.startAnimating()
//結(jié)束
imageView1.stopAnimating()
//添加點(diǎn)擊事件
UIImageView和UILabel類(lèi)似,其用戶(hù)交互默認(rèn)關(guān)閉垢粮,我們要給其添加點(diǎn)擊事件贴届,需要打開(kāi)其用戶(hù)交互。
imageView1.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(ViewController.tapGestureRecognizer(sender:)))
imageView1.addGestureRecognizer(tapGestureRecognizer)
func tapGestureRecognizer(sender:UITapGestureRecognizer) {
//code
}
//屬性設(shè)置
//邊框設(shè)置
imageView.layer.borderColor = UIColor.redColor().CGColor
imageView.layer.borderWidth = 2
//圓角的設(shè)置
imageView.layer.cornerRadius = 150
imageView.layer.masksToBounds = true
UIImageView繼承UIView蜡吧,很多常見(jiàn)的屬性在此就不列舉毫蚓。。