码迷,mamicode.com
首页 > 编程语言 > 详细

Swift小代码

时间:2015-03-06 19:04:07      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 

iOS常见控件的创建方法

import UIKit

class ViewController: UIViewController {

    

    var myLabel: UILabel = UILabel()

    var myView: UIView = UIView()

    var myButton: UIButton = UIButton()

    var myImageView: UIImageView = UIImageView()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        // Do any additional setup after loading the view.

        self.view.backgroundColor = UIColor.redColor()

        

        self.myLabel = createLabel()

        self.view.addSubview(self.myLabel)

        

        self.myView = createView()

        self.view.addSubview(self.myView)

        

        self.myButton = createButton()

        self.view.addSubview(self.myButton)

        

        self.myImageView = createImageView()

        self.view.addSubview(self.myImageView)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    // UILabel

    func createLabel() -> UILabel {

        var label: UILabel = UILabel(frame: CGRectMake(10, 80, self.view.frame.size.width-20, 50))

        label.backgroundColor = UIColor.greenColor()

        label.textAlignment = NSTextAlignment.Center

        label.text = "Hello Swift"

        return label

    }

    

    // UIView

    func createView() -> UIView {

        var orginY = CGRectGetMaxY(self.myLabel.frame) + 10

        var myView: UIView = UIView(frame: CGRectMake(10, orginY, self.view.frame.size.width-20, 30))

        myView.backgroundColor = UIColor.whiteColor()

        return myView;

    }

    

    // UIButton

    func createButton() -> UIButton {

        var orginY = CGRectGetMaxY(self.myView.frame) + 10

        var button: UIButton = UIButton(frame: CGRectMake(10, orginY, self.view.frame.size.width-20, 30))

        button.backgroundColor = UIColor.greenColor()

        button.setTitle("Button", forState: UIControlState.Normal)

        button.titleLabel?.font = UIFont.systemFontOfSize(18)

        button.addTarget(self, action: "tappedButton:", forControlEvents: UIControlEvents.TouchUpInside)

        button.tag = 100

        return button

    }

    

    // UIImageView

    func createImageView() -> UIImageView {

        var orginY = CGRectGetMaxY(self.myButton.frame) + 10

        var imageView: UIImageView = UIImageView(frame: CGRectMake((self.view.frame.size.width-100)/2, orginY, 100, 50))

        var image: UIImage = UIImage(named: "user")!

        imageView.image = image

        return imageView

    }

    

    // Button target

    func tappedButton(sender:UIButton!) {

        println(sender.tag)

    }

}

 

 

 

 单列

import Foundation

class UtilSingleton: NSObject {

    

    var iVal: Int = 0

    

    class var shareInstance: UtilSingleton {

        get {

            struct Static {

                static var instance: UtilSingleton? = nil

                static var token: dispatch_once_t = 0

            }

            dispatch_once(&Static.token, {

                Static.instance = UtilSingleton()

            })

            return Static.instance!

        }

    }

}

 

 

 

二维码扫描

import UIKit

import Foundation

import AVFoundation

class ViewController: UIViewController ,AVCaptureMetadataOutputObjectsDelegate {

    

    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    let session = AVCaptureSession()

    var layer: AVCaptureVideoPreviewLayer?

     

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        self.title = "二维码扫描"

        self.view.backgroundColor = UIColor.grayColor()

    }    

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)

        self.setupCamera()

        self.session.startRunning()

    }

 

    func setupCamera(){

        self.session.sessionPreset = AVCaptureSessionPresetHigh

        var error : NSError?

        let input = AVCaptureDeviceInput(device: device, error: &error)

        if (error != nil) {

            println(error!.description)

            return

        }

        if session.canAddInput(input) {

            session.addInput(input)

        }

        layer = AVCaptureVideoPreviewLayer(session: session)

        layer!.videoGravity = AVLayerVideoGravityResizeAspectFill

        layer!.frame = CGRectMake(20,150,280,280);

        self.view.layer.insertSublayer(self.layer, atIndex: 0)

        let output = AVCaptureMetadataOutput()

        output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

        if session.canAddOutput(output) {

            session.addOutput(output)

            output.metadataObjectTypes = [AVMetadataObjectTypeQRCode];

        }

        

        session.startRunning()

    }

    

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!){

        var stringValue:String?

        if metadataObjects.count > 0 {

            var metadataObject = metadataObjects[0] as AVMetadataMachineReadableCodeObject

            stringValue = metadataObject.stringValue

        }

        self.session.stopRunning()

        println("code is \(stringValue)")

 

        var alertView = UIAlertView()

        alertView.delegate=self

        alertView.title = "二维码"

        alertView.message = "扫到的二维码内容为:\(stringValue)"

        alertView.addButtonWithTitle("确认")

        alertView.show()

    }

}

 

Swift小代码

标签:

原文地址:http://www.cnblogs.com/li-Xu/p/4318806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!