• 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开发!

     
     
     
  • 相关阅读:
    02点睛Spring4.1-Java Config
    01点睛Spring4.1-依赖注入
    00点睛Spring4.1-环境搭建
    Cas(09)——通过Proxy访问其它Cas应用
    Cas(08)——单点登出
    Cas(07)——建立使用Cas进行单点登录的应用
    Cas(06)——基于数据库的认证
    Cas(05)——修改Cas Server的其它配置
    remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
    解放双手—Cobbler批量自动化部署多版本系统
  • 原文地址:https://www.cnblogs.com/ITmgc/p/4462664.html
Copyright © 2020-2023  润新知