• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift小代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

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()

    }

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用纯swift代码文件制作framework发布时间:2022-07-13
下一篇:
从swift-init main start 开始看swift代码发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap