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学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
随机推荐
- AD 删除一个区域里的所有布线,不删除元器件,丝印等
想删除一个区域里的所有布线,不删除元器件,丝印等 按F12后输入以下组合:isvia or(istrack and OnTopLayer) or (istrack and OnBottomLayer) ...
- Altium designer PCB中过孔铺地连接的设置
在Altium designer 6及更高版本如Altium Designer Winter 9.altium designer summer 9都会有这样的问题,在Altium DXP2004里面是 ...
- css属性之!important
提升指定样式规则的应用优先权. IE6及以下浏览器有个比较显式的支持问题存在,!important在同一条规则集里不生效.请看下述代码: div { color: #f00 !important; c ...
- BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏
3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 51 Solved: 4 ...
- iOS 9之Safari广告拦截器(Content Blocker)
金田( github 示例源码) 相对于谷歌对广告拦截的禁止,苹果与之态度截然相反,继Mac版Safari加入广告拦截工具之后,即将到来的iOS9对Safari也引入了内容拦截插件-Content B ...
- 计算新浪Weibo消息长度
此文为计算新浪Weibo的消息长度的方法. 就是 (发言请遵守社区公约,还可以输入119字). var getMessageLength = (function() { var byteLength ...
- (总结)工作中常用的js自定义函数——日期时间类
//设置时间类 var Wsdatatime = function(){ this.today = (new Date()).getTime(); //当前时间 } Wsdatatime.protot ...
- bfs 记录和打印最短路径
Poj3984 迷宫问题 #include <iostream> #include <algorithm> #include <cstdio> #include & ...
- zoj3422Go Deeper(2-sat + 二分)
题目请戳这里 题目大意: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + ...
- PE基金的运作模式有哪些?
一.信托制(1)信托型基金是由基金管理机构与信托公司合作设立,通过发起设立信托受益份额募集资金,然后进行投资运作的集合投资工具(2)信托公司和基金管理机构组成决策委员会实施,共同进行决策(3)在内部分 ...