Swift 几个简单控件及上传系统相册照片
2016-05-17 14:10
489 查看
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.
}
}
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.
}
}
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- 在Android开发中使用自定义组合控件的例子
- Android重写View实现全新的控件
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- WinForm拖拽控件生成副本的解决方法
- ASP.NET动态添加用户控件的方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- WinForm实现移除控件某个事件的方法
- C#分屏控件用法实例