对于习惯了OC代码的程序员来说,swift的语法简直让人不能忍受,今天将一些常用的UI控件简单做了一下整理。
importUIKit
classViewController:UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{
overridefuncviewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.whiteColor()
ifself.title=="UILabel"
{
// Label
letlabel=UILabel(frame:self.view.bounds)
label.backgroundColor=UIColor.clearColor()
label.textAlignment=NSTextAlignment.Center
label.font=UIFont.systemFontOfSize(36)
label.text="Hello, Swift"
self.view.addSubview(label)
}
elseifself.title=="UIButton"
{
// Button
letbutton=UIButton(type:UIButtonType.System)
button.frame=CGRectMake(110,120,100,50)
button.backgroundColor=UIColor.grayColor()
button.setTitleColor(UIColor.redColor(),forState:UIControlState.Normal)
button.setTitleColor(UIColor.whiteColor(),forState:UIControlState.Highlighted)
button.setTitle("普通",forState:UIControlState.Normal)
button.setTitle("高亮",forState:UIControlState.Highlighted)
button.addTarget(self,action:"buttonAction:",forControlEvents:UIControlEvents.TouchUpInside)
button.tag=100
self.view.addSubview(button)
}
elseifself.title=="UIImageView"
{
letimage=UIImage(named:"swift-hero.png")
letimageView=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)
}
elseifself.title=="UISlider"
{
letslider=UISlider(frame:CGRectMake(60,120,200,30))
self.view.addSubview(slider)
}
elseifself.title=="UIWebView"
{
letwebView=UIWebView(frame:self.view.bounds)
leturl=NSURL(string:"http://caipiao.m.taobao.com")
letrequest=NSURLRequest(URL:url!)
webView.loadRequest(request)
self.view.addSubview(webView)
}
elseifself.title=="UISegmentedControl"
{
letsegmentControl=UISegmentedControl(items:["A","B","C","D"])
segmentControl.frame=CGRectMake(110,120,100,30)
self.view.addSubview(segmentControl)
}
elseifself.title=="UISwitch"
{
letswitchControl=UISwitch(frame:CGRectMake(130,120,100,30))
switchControl.on=true
self.view.addSubview(switchControl)
}
elseifself.title=="UITextField"
{
lettextField=UITextField(frame:CGRectMake(60,120,200,30))
textField.backgroundColor=UIColor.lightGrayColor()
textField.placeholder="请输入"
self.view.addSubview(textField)
}
elseifself.title=="UIScrollView"
{
letscrollView=UIScrollView(frame:CGRectMake(60,120,200,200))
scrollView.pagingEnabled=true
scrollView.showsVerticalScrollIndicator=false
self.view.addSubview(scrollView)
varfX:CGFloat=0.0
for(vari=0;i<3; ++i)
{
letview=UIView(frame:CGRectMake(fX,0,200,200))
fX+=200
view.backgroundColor=UIColor(red:CGFloat(100*i) /256,green:CGFloat(100*i) /256,blue:CGFloat(100*i) /256,alpha:1)
// 这个地方因为swift对于类型的控制十分严格,颜色值采用的是CGFloat类型,但是i是int类型,所以需要强转
scrollView.addSubview(view)
}
scrollView.contentSize=CGSizeMake(3*200,0)
self.view.addSubview(scrollView)
}
elseifself.title=="UISearchBar"
{
letsearchBar=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)
}
elseifself.title=="UIPageControl"
{
// PageControl
letpageControl=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)
}
elseifself.title=="UIDatePicker"
{
letdatePicker=UIDatePicker(frame:CGRectMake(0.0,120.0,200.0,200.0))
self.view.addSubview(datePicker)
}
elseifself.title=="UIPickerView"
{
letpickerView=UIPickerView(frame:CGRectMake(10.0,120.0,300.0,200.0))
pickerView.delegate=self
pickerView.dataSource=self
self.view.addSubview(pickerView)
}
elseifself.title=="UIProgressView"
{
letprogressView=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)
}
elseifself.title=="UITextView"
{
lettextView=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)
}
elseifself.title=="UIToolbar"
{
lettoolBar=UIToolbar(frame:CGRectMake(60.0,120.0,200.0,30.0))
letflexibleSpace=UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.FlexibleSpace,target:nil,action:nil)
letbarBtnItemA=UIBarButtonItem(title:"A",style:UIBarButtonItemStyle.Plain,target:nil,action:nil)
letbarBtnItemB=UIBarButtonItem(title:"B",style:UIBarButtonItemStyle.Plain,target:nil,action:nil)
letbarBtnItemC=UIBarButtonItem(title:"C",style:UIBarButtonItemStyle.Plain,target:nil,action:nil)
letbarBtnItemD=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)
}
elseifself.title=="UIActionSheet"
{
// Button
letbutton=UIButton(type:UIButtonType.System)
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("ActionSheet",forState:UIControlState.Normal)
button.setTitle("ActionSheet",forState:UIControlState.Highlighted)
button.addTarget(self,action:"showActionSheet",forControlEvents:UIControlEvents.TouchUpInside)
button.tag=101
self.view.addSubview(button)
}
elseifself.title=="UIActivityIndicatorView"
{
letactivityIndicatorView=UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.Gray)
activityIndicatorView.frame=CGRectMake(140.0,120.0,40.0,40.0)
activityIndicatorView.startAnimating()
self.view.addSubview(activityIndicatorView)
}
}
funcbuttonAction(sender:UIButton) {
letalertController=UIAlertController(title:"通知",message:"确定还是取消",preferredStyle:UIAlertControllerStyle.Alert)
letalertView1=UIAlertAction(title:"确定",style:UIAlertActionStyle.Default) { (UIAlertAction) ->Voidin
print("确定按钮点击事件")
}
letalertView2=UIAlertAction(title:"取消",style:UIAlertActionStyle.Default) { (UIAlertAction) ->Voidin
print("取消按钮点击事件")
}
letalertView3=UIAlertAction(title:"下次吧",style:UIAlertActionStyle.Default) { (UIAlertAction) ->Voidin
print("下次吧按钮点击事件")
}
alertController.addAction(alertView1)
alertController.addAction(alertView2)
alertController.addAction(alertView3)
self.presentViewController(alertController,animated:true,completion:nil)
}
funcshowActionSheet()
{
letalertController=UIAlertController(title:"通知",message:"确定还是取消",preferredStyle:UIAlertControllerStyle.ActionSheet)
letalertView1=UIAlertAction(title:"确定",style:UIAlertActionStyle.Default) { (UIAlertAction) ->Voidin
print("确定按钮点击事件")
}
letalertView2=UIAlertAction(title:"取消",style:UIAlertActionStyle.Default) { (UIAlertAction) ->Voidin
print("取消按钮点击事件")
}
letalertView3=UIAlertAction(title:"下次吧",style:UIAlertActionStyle.Default) { (UIAlertAction) ->Voidin
print("下次吧按钮点击事件")
}
alertController.addAction(alertView1)
alertController.addAction(alertView2)
alertController.addAction(alertView3)
self.presentViewController(alertController,animated:true,completion:nil)
}
// UIPickerViewDataSource Methods
// returns the number of 'columns' to display.
funcnumberOfComponentsInPickerView(pickerView:UIPickerView) ->Int
{
return3
}
// returns the # of rows in each component..
funcpickerView(pickerView:UIPickerView,numberOfRowsInComponentcomponent:Int) ->Int
{
return10
}
funcpickerView(pickerView:UIPickerView,titleForRowrow:Int,forComponentcomponent:Int) ->String?
{
returnString(format:"%i",row)
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
网友评论