[置顶] Swift基础之UIImageView(都是2.2版本)

//设置全局变量,将下面的替换即可
    //var myImgView = UIImageView();
    //系统生成的viewDidLoad()方法
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //创建局部变量UITextField
        let myImgView = UIImageView.init(frame: CGRectMake(80, 100, 100, 100));
        //设置背景颜色
        myImgView.backgroundColor = UIColor.cyanColor();
        //添加图片
        myImgView.image = UIImage(named: "per_info_boy.png");
        //设置图片边框
        myImgView.layer.borderWidth = 2;
        //设置边框颜色
        myImgView.layer.borderColor = UIColor.lightGrayColor().CGColor;
        //设置圆角
        myImgView.layer.cornerRadius = 50;
        //设置是否裁剪多余边缘
        myImgView.clipsToBounds = true;
        //设置图片的触摸事件(手势)
        let myTapGesture = UITapGestureRecognizer.init(target: self, action: #selector(imgTouch));
        //设置图片是否与用户交互
        myImgView.userInteractionEnabled = true;
        //设置图片的手势
        myImgView.addGestureRecognizer(myTapGesture);
        
        
        //添加视图
        self.view.addSubview(myImgView);
    }
    //实现触摸事件方法
    func imgTouch(sender:UITapGestureRecognizer!)
    {
        print("触发了图片事件方法....");
        //2.2版本之后开发
        let imgV:UIImageView = sender.view as! UIImageView;
        
        imgV.image = UIImage(named: "per_info_girl.png");
    }

你可能感兴趣的:(swift,UIImageView)