swift4.0UI控件使用

  //UIview的使用

        self.view.backgroundColor = UIColor.green

        let view = UIView(frame: CGRect.init(x: 100, y: 100, width: 100, height: 100))

        view.backgroundColor = UIColor.red

        self.view.addSubview(view)

   let imageView = UIImageView(frame: CGRect.init(x: 100, y: 100, width: 100, height: 100))

    imageView.image = UIImage(named: "qwe")

    let path = Bundle.main.path(forResource: "qwert", ofType: "jpg")

    imageView.image = UIImage(contentsOfFile: path!)

    

    //let url = URL(string: "http://pic150.nipic.com/file/20171224/8669400_090903351033_2.jpg")

    //从网络获取数据流

    // let data = try! Data(contentsOf: url!)

    //通过数据流初始化图片

    // let newImage = UIImage(data: data)

    imageView.layer.masksToBounds = true

    imageView.layer.cornerRadius = 10

    imageView.isUserInteractionEnabled = true

    let tap = UITapGestureRecognizer.init(target: self, action:#selector(ViewController.tapAction));

    imageView.addGestureRecognizer(tap);

    //定义URL对象

    //        let url = URL(string: "http://hangge.com/blog/images/logo.png")

    //        //从网络获取数据流

    //        let data = try! Data(contentsOf: url!)

    //        //通过数据流初始化图片

    //        let newImage = UIImage(data: data)

    //        imageView.image = newImage;

    self.view.addSubview(imageView);

     // SWIFT 提倡:

        // Replace 'Selector("buttonTap")' with '#selector(ViewController.buttonTap)'

        

        // button  点击无参数5

        let button = UIButton(frame: CGRect(x: 50, y: 50, width: 50, height: 50))

        button.backgroundColor = UIColor.yellow

        button.addTarget(self, action: #selector(ViewController.buttonTap), for: UIControl.Event.touchUpInside)

        //button1:点击有参数

        let button1 = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 50))

        button1.backgroundColor = UIColor.green

        button1.addTarget(self, action: #selector(buttonTap1(button:)), for: UIControl.Event.touchUpInside)

        self.view.addSubview(button)

        self.view.addSubview(button1)

  //selector 其实是 Objective-C runtime 的概念

    @objc func buttonTap() {

        print("buttonTap")

    }

    

    @objc func buttonTap1(button:UIButton) {

        print("buttonTap参数")

    }

    

    

    

@objc func tapAction(tap:UITapGestureRecognizer) {

        

        print("图片点击了")

        

        

    }

猜你喜欢

转载自blog.csdn.net/a18339063397/article/details/82758935