您的位置:首页 > 移动开发 > Swift

Swift UIImageView 构造方法

2015-09-09 10:53 435 查看
UIImageView有三个构造方法

init(frame:CGRect)
我们可能发现UIImageView并没有这个构造方法,其实他是继承父类UIView的方法

//1,init(frame:CGRect)
        let imgV=UIImageView(frame: CGRectMake(20, 50, 100, 100))
        let img=UIImage(named: "test0.png")
        imgV.image=img
        self.view .addSubview(imgV)


init(image:UIImage!)

这个比较简单直接看代码

//2,init(image:UIImage)
        let imgV1=UIImageView(image: img)
        imgV1.frame=CGRectMake(20, 200, 100, 100)
        self.view .addSubview(imgV1)


init(image:UIImage!, highlightedImage:
UIImage?)

//3,init(image:UIIImage!,highlightedImage:UIImage?)
        let imgV2=UIImageView(image: img, highlightedImage: UIImage(named: "test1.png"))
        imgV2.frame=CGRectMake(20, 350, 100, 100)
        self.view.addSubview(imgV2)
        
        //第三个构造方法的第二参数为imageView高亮的时候会现实的图片。这里我们手动模拟一下直接设置为高亮看效果
        imgV2.highlighted=true


苹果开发群 :414319235 欢迎加入 欢迎讨论问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: