Swift学习之常用UI的使用
Swift学习之常用UI的使用
最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑)。
所以在不久的将来,swift绝对是iOS 开发的主导语言,也许不会完全取代OC。
笔者学完swift的语法之后就开始着手UI了,因为我觉得有着一定的OC基础。所以这里关于swift的语法就不做多介绍了,在后面的文章中,我将会详细介绍一下关于swift中的重点,难点语法和一些新特性。
下面是我在学习UI的时候自己总结的一些swift创建UI的代码,个人感觉和OC区别不到,但是还是有需要注意的地方。
override func viewDidLoad()
{
self.view!.backgroundColor = UIColor.whiteColor()
/**
* 1******************** UILabel
*/
if self.title == "UILabel"
{
var label = UILabel(frame: self.view.bounds)
//这里也可以先创建再设置frame
label.backgroundColor = UIColor.clearColor()
label.textAlignment = NSTextAlignment.Center
label.font = UIFont.systemFontOfSize(36)
label.text = "Hello, Swift"
self.view.addSubview(label)
}
/**
* 2********************UIButton
*/
else if self.title == "UIButton"
{
var button = UIButton.buttonWithType(UIButtonType.System) as? UIButton
button!.frame = CGRectMake(110.0, 120.0, 100.0, 50.0)
button!.backgroundColor = UIColor.grayColor()
button?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
button!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
button?.setTitle("Touch Me", forState: UIControlState.Normal)
button?.setTitle("Touch Me", forState: UIControlState.Highlighted)
button?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button!.tag = 100
self.view.addSubview(button!)
}
/**
* 3******************** UIImageView
*/
else if self.title == "UIImageView"
{
var image = UIImage(named: "swift-hero.png")
var imageView = UIImageView(frame:
CGRectMake((CGRectGetWidth(self.view.bounds) - image!.size.width) / 2.0,
120.0, image!.size.width, image!.size.height))
imageView.image = image
self.view.addSubview(imageView)
}
/**
* 4********************UISilder
*/
else if self.title == "UISlider"
{
var slider = UISlider(frame:CGRectMake(60.0, 120.0, 200.0, 30.0))
self.view.addSubview(slider)
}
/**
* 5******************** UIWebView
*/
else if self.title == "UIWebView"
{
var webView = UIWebView(frame:self.view.bounds)
var url = NSURL(string: "http://caipiao.m.taobao.com")
var request = NSURLRequest(URL: url!)
webView.loadRequest(request)
self.view.addSubview(webView)
}
/**
* 6********************UISegmentedControl
*/
else if self.title == "UISegmentedControl"
{
var segmentControl = UISegmentedControl(items:["A", "B", "C", "D"])
segmentControl.frame = CGRectMake(110.0, 120.0, 100.0, 30.0)
self.view.addSubview(segmentControl)
}
/**
* 7********************UISwitch
*/
else if self.title == "UISwitch"
{
var switchControl = UISwitch(frame:CGRectMake(130.0, 120.0, 100.0, 30.0))
switchControl.on = true
self.view.addSubview(switchControl)
}
/**
* 8********************UItxetField
*/
else if self.title == "UITextField"
{
var textField = UITextField(frame:CGRectMake(60.0, 120.0, 200.0, 30.0))
textField.backgroundColor = UIColor.lightGrayColor()
textField.placeholder = "input text"
self.view.addSubview(textField)
}
/**
* 9********************UIScrollView
*/
else if self.title == "UIScrollView"
{
var scrollView = UIScrollView(frame:CGRectMake(60.0, 120.0, 200.0, 200.0))
scrollView.pagingEnabled = true
scrollView.showsVerticalScrollIndicator = false
self.view.addSubview(scrollView)
var fX: CGFloat = 0.0
for(var i = 0; i < 3; ++i)
{
var view = UIView(frame:CGRectMake(fX, 0.0, 200.0, 200.0))
fX += 200.0
view.backgroundColor = UIColor.redColor()
scrollView.addSubview(view)
}
scrollView.contentSize = CGSizeMake(3 * 200.0, 200.0)
self.view.addSubview(scrollView)
}
/**
* 10********************UISearchBar
*/
else if self.title == "UISearchBar"
{
var searchBar = UISearchBar(frame:CGRectMake(10.0, 120.0, 300.0, 30.0))
searchBar.showsCancelButton = true
searchBar.searchBarStyle = UISearchBarStyle.Minimal // Default, Prominent, Minimal
self.view.addSubview(searchBar)
}
/**
* 11********************UIpageControl
*/
else if self.title == "UIPageControl"
{
// PageControl
var pageControl = UIPageControl(frame:CGRectMake(60.0, 120.0, 200.0, 200.0))
pageControl.numberOfPages = 5
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
pageControl.pageIndicatorTintColor = UIColor.redColor()
self.view.addSubview(pageControl)
}
/**
* 12********************UIDatePicker
*/
else if self.title == "UIDatePicker"
{
var datePicker = UIDatePicker(frame:CGRectMake(0.0, 120.0, 200.0, 200.0))
self.view.addSubview(datePicker)
}
/**
* 13******************** UIPickerView
*/
else if self.title == "UIPickerView"
{
var pickerView = UIPickerView(frame:CGRectMake(10.0, 120.0, 300.0, 200.0))
pickerView.delegate = self
pickerView.dataSource = self
self.view.addSubview(pickerView)
}
/**
* 14********************UIProgressView
*/
else if self.title == "UIProgressView"
{
var progressView = UIProgressView(progressViewStyle:UIProgressViewStyle.Default)
progressView.frame = CGRectMake(10.0, 120.0, 300.0, 30.0)
progressView.setProgress(0.8, animated: true)
self.view.addSubview(progressView)
}
/**
* 15******************** UITextView
*/
else if self.title == "UITextView"
{
var textView = UITextView(frame:CGRectMake(10.0, 120.0, 300.0, 200.0))
textView.backgroundColor = UIColor.lightGrayColor()
textView.editable = false
textView.font = UIFont.systemFontOfSize(20)
textView.text = "Swift is an innovative new programming language for
Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax
is concise yet expressive, and apps run lightning-fast. Swift is ready
for your next iOS and OS X project — or for addition into your current
app — because Swift code works side-by-side with Objective-C."
self.view.addSubview(textView)
}
/**
* 16********************UIToolbar
*/
else if self.title == "UIToolbar"
{
var toolBar = UIToolbar(frame:CGRectMake(60.0, 120.0, 200.0, 30.0))
var flexibleSpace = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.FlexibleSpace, target:nil, action:nil)
var barBtnItemA = UIBarButtonItem(title: "A", style:UIBarButtonItemStyle.Plain, target:nil, action:nil)
var barBtnItemB = UIBarButtonItem(title: "B", style:UIBarButtonItemStyle.Plain, target:nil, action:nil)
var barBtnItemC = UIBarButtonItem(title: "C", style:UIBarButtonItemStyle.Plain, target:nil, action:nil)
var barBtnItemD = UIBarButtonItem(title: "D", style:UIBarButtonItemStyle.Plain, target:nil, action:nil)
toolBar.items = [flexibleSpace, barBtnItemA, flexibleSpace,
barBtnItemB, flexibleSpace, barBtnItemC, flexibleSpace, barBtnItemD,
flexibleSpace]
self.view.addSubview(toolBar)
}
/**
* 16******************** UIActionSheet
*/
else if self.title == "UIActionSheet"
{
// Button
var button = UIButton.buttonWithType(UIButtonType.System) as? UIButton
button!.frame = CGRectMake(60.0, 120.0, 200.0, 50.0)
button!.backgroundColor = UIColor.grayColor()
button?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
button!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
button?.setTitle("Show ActionSheet", forState: UIControlState.Normal)
button?.setTitle("Show ActionSheet", forState: UIControlState.Highlighted)
button?.addTarget(self, action: "showActionSheet", forControlEvents: UIControlEvents.TouchUpInside)
button!.tag = 101
self.view.addSubview(button!)
}
/**
* 17********************UIActivityIndicatorView
*/
else if self.title == "UIActivityIndicatorView"
{
var activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.Gray)
activityIndicatorView.frame = CGRectMake(140.0, 120.0, 40.0, 40.0)
activityIndicatorView.startAnimating()
self.view.addSubview(activityIndicatorView)
}
else
{}
}
下面是一些UI中对应按钮的target,和一些需要实现的方法:
override func viewWillAppear(animated: Bool) {}
override func viewDidAppear(animated: Bool) {}
override func viewWillDisappear(animated: Bool) {}
override func viewDidDisappear(animated: Bool) {}
// Button Action
func buttonAction(sender: UIButton)
{
var mathSum = MathSum()
var sum = mathSum.sum(11, number2: 22)
var alert = UIAlertController(title: "Title", message: String(format:
"Result = %i", sum), preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
/*
var alertView = UIAlertView()
alertView.title = "Title"
alertView.message = "Message"
alertView.addButtonWithTitle("OK")
alertView.show()
*/
}
// Button Handler
// showActionSheet
func showActionSheet()
{
var alertController = UIAlertController(title: "ActionSheet", message:
"Message", preferredStyle: UIAlertControllerStyle.ActionSheet)
alertController.addAction(UIAlertAction(title: "Go Back", style: UIAlertActionStyle.Destructive, handler: nil))
self.presentViewController(alertController, animated: true, completion:nil)
}
// didReceiveMemoryWarning
override func didReceiveMemoryWarning()
{
/********/
}
后面的文章里,笔者将给大家打来使用swift开发项目的一些常用技术和区分swift与OC开发!
Swift学习之常用UI的使用的更多相关文章
- JQuery 入门学习附常用UI模块
一.简介 jQuery 库可以通过一行简单的标记被添加到网页中 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操 ...
- 从Swift学习iOS开发的路线指引
本文主要是楼主近段时间从Swift语法开始自学iOS开发的流程总结,PS 一个前提,楼主的生存环境中买不到一本iOS开发中文教程,所以基本都是百度摸索出来的 >_< 主要流程 学习Swif ...
- iOS ---Swift学习与复习
swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...
- swift学习(二)--基本运算符、字符串、集合操作
在这一篇博客里面,我想要介绍一下swift里面一些常用的基本运算符,还有涉及到的字符串,集合操作.你会发现在swift里面还是有许多其他语言所不具有的特性运算操作的. 首先最基本的+,-,*,/,&g ...
- Swift学习初步(一)
前几天刚刚将有关oc的教程草草的看了一遍,发现oc其实也不像传说的那么难.今天又开始马不停蹄的学习Swift因为我很好奇,到底苹果出的而且想要代替oc的编程语言应该是个什么样子呢?看了网上的一些中文教 ...
- IOS开发-OC学习-常用功能代码片段整理
IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- Swift学习与复习
swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...
- 【swift学习笔记】二.页面转跳数据回传
上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
随机推荐
- Update主循环、状态机的实现
从写一段程序,到写一个app,写一个游戏,到底其中有什么不同呢?一段程序的执行时间很短,一个应用的执行时间很长,仅此而已. 游戏中存在一个帧的概念. 这个概念大家都知道,类比的话,它就是电影胶卷的 ...
- PYTHONE的WHILE,BREAK,CONTINUE示例
简短示例: while True: s = raw_input('Enter something : ') if s == 'quit': break if len(s) < 3: print ...
- 在QT程序中使用cout和cin
1先输入10个数字,再输出. #include <QtCore/QCoreApplication> #include <QtCore/QList> #include <Q ...
- git设置过滤忽略的文件或文件夹
我们一般向代码仓库提交项目的时候,一般需要忽略编译生成的中间文件以及文件夹的提交,因为它们是无用的,而且也会占用仓库的空间.一般只用提交.pro,.sln,makefile,程序源文件等编译必须用到的 ...
- 【HDOJ】4972 A simple dynamic programming problem
水题. #include <cstdio> #include <cstring> #include <cstdlib> int abs(int x) { ? -x: ...
- vbox端口转发
端口转发:setting->network->adapter:attached to NAT.port forwarding rules->name protocol ...
- bzoj3995[SDOI2015]道路修建
http://www.lydsy.com/JudgeOnline/problem.php?id=3995 线段树维护连通性. 我们发现,对于一个区间[L,R],我们只需要知道(1,L),(2,L),( ...
- Android客户端实现七牛云存储文件上传
1.简单文件上传 上传模型如下. 1.1获得Token 不管是简单文件上传,还是分片上传.断点续传 都需要首先访问服务器,以获得上传凭证信息Token..用于测试时,可以用本地模拟Token信息(有 ...
- 5s
整理,整顿,清楚,清洁,身美
- 【转】编写Chrome扩展程序
Chrome的扩展程序很多,也很容易入门,可以来简单实现一下 看看,慢慢就能实现出一个扩展程序来 每个扩展程序应用一般会包含: 一个manifest清单文件 html文件 js文件 其他文件等 可以看 ...