Swift 几个简单控件及上传系统相册照片

import UIKit


class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    let inputNameLabel = UILabel()

    let inputNameTextField = UITextField()

    let inputNameLoginButton = UIButton()

    let photo = UIImageView()

    

    

    override func viewDidLoad() {

        super.viewDidLoad()


        //输入信息名称

        inputNameLabel.frame = CGRect(x: 20, y: 50, width: 200, height: 20)

        inputNameLabel.text = "Input Your Name"

        inputNameLabel.textColor = UIColor.blueColor()

        inputNameLabel.font = UIFont.systemFontOfSize(14)

        inputNameLabel.textAlignment = NSTextAlignment.Left

        self.view.addSubview(inputNameLabel)

        

        //输入信息

        inputNameTextField.frame = CGRect(x: 20, y: 90, width: 200, height: 30)

        inputNameTextField.placeholder = " Please Input Your Name"

        inputNameTextField.font = UIFont.systemFontOfSize(14)

        inputNameTextField.delegate = self

        inputNameTextField.clipsToBounds = true

        inputNameTextField.layer.cornerRadius = 5

        inputNameTextField.layer.borderWidth = 0.5

        inputNameTextField.layer.borderColor = UIColor.grayColor().CGColor

        self.view.addSubview(inputNameTextField)

        

        //模拟登陆按钮

        inputNameLoginButton.frame = CGRect(x: 20, y: 130, width: 200, height: 30)

        inputNameLoginButton.setTitle("Login Button", forState: UIControlState.Normal)

        inputNameLoginButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

        inputNameLoginButton.titleLabel?.font = UIFont.systemFontOfSize(14)

        inputNameLoginButton.titleLabel?.textAlignment = NSTextAlignment.Left

        inputNameLoginButton.addTarget(self, action: #selector(ViewController.loginClike), forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(inputNameLoginButton)

        

        //照片

        photo.frame = CGRect(x: 20, y: 180, width: 200, height: 200)

        photo.image = UIImage(named: "img.jpg")

        //给照片加入点击事件  点击可选择系统相册照片

        let choosePhoto: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector (ViewController.chooseTap))

        photo.addGestureRecognizer(choosePhoto)

        photo.userInteractionEnabled = true

        self.view.addSubview(photo)

        

    }

    

    func loginClike() -> () {

        print("Login Button Clike...")

        inputNameLabel.text = "Change Input Your Name"

        photo.image = UIImage(named: "img1.jpg")

        

    }

    

    func chooseTap() -> () {

        

        //创建UIImagePickerController

        let imagePicker = UIImagePickerController()

        //类型 。。 相册

        imagePicker.sourceType = .PhotoLibrary

        imagePicker.delegate = self//设置代理

        presentViewController(imagePicker, animated: true, completion: nil)//跳转

        

    }


    //点击取消  返回

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {

        dismissViewControllerAnimated(true, completion: nil)

    }

    

    //选择完照片后的调用方法

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        //拿到选择完的照片

        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        //设置photo的照片

        photo.image = selectedImage

        //返回

        dismissViewControllerAnimated(true, completion: nil)

     }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值