文档章节

swift 地图应用开发 --- 二维码扫描

温红权
 温红权
发布于 2015/04/29 12:38
字数 771
阅读 17
收藏 0

二维码扫描:

swift二维码扫描

修改:

<!-- lang: cpp -->
import Foundation
import AVFoundation
import QuartzCore
class ReadQrCodeController: BaseController {

private var codeReader: QRCodeReader?


@IBOutlet weak var topview: UIView!


var looper:CADisplayLink?

private var line_1 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_2 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_3 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_4 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_5 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_6 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_7 = CommonUtil.newLineOverlay(0x2bcd9c)
private var line_8 = CommonUtil.newLineOverlay(0x2bcd9c)

private var gradientLayer:CAGradientLayer?

func backtomain(){
    
    self.loading?.dismissAnimated(false)
    self.loading?.dismiss()
    
    CommonUtil.transitionWithType(kCATransitionReveal, withSubType: kCATransitionFromBottom, forView: self.view.window!)
    
    self.navigationController?.popViewControllerAnimated(false)
}

var loading:JGProgressHUD?

override func viewDidLoad() {
    
    
    let reader = QRCodeReader(metadataObjectTypes: [AVMetadataObjectTypeQRCode])

    codeReader           = reader
    view.backgroundColor = UIColor.blackColor()
    
    codeReader?.completionBlock = { [unowned self] (resultAsString) in
        
        
        if(resultAsString != nil){
            
            var myStringArr:[String] = resultAsString!.componentsSeparatedByString("/")
            
            if( myStringArr.count > 0 ){
                

                
                
                
                
            }
        
        }
        
       
        
        

        
    }
    
    setupUIComponentsWithCancelButtonTitle()
    

    
    topview.layer.insertSublayer(codeReader!.previewLayer, atIndex: 0)
    
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationDidChanged:", name: UIDeviceOrientationDidChangeNotification, object: nil)
    
    

    let shapeLayer = CommonUtil.newLineOverlay()
    shapeLayer.frame = view.layer.frame
    var recttemp:CGRect = CGRect(x:(view.layer.frame.size.width - 220) / 2, y: 65, width: 220, height: 220)
    var path:UIBezierPath = UIBezierPath(roundedRect: recttemp
        , cornerRadius: 0)
    
    shapeLayer.masksToBounds = true
    
    var rect = view.layer.frame
    
    path.appendPath(UIBezierPath(rect: rect))
    shapeLayer.path = path.CGPath
    shapeLayer.fillRule = kCAFillRuleEvenOdd
    
    let overlay = CommonUtil.newLineOverlayDefult()
    overlay.path = UIBezierPath(roundedRect: recttemp
        , cornerRadius: 0).CGPath
    
    
    topview.layer.addSublayer(shapeLayer)
    topview.layer.addSublayer(overlay)


    
    
    var recttemp1:CGRect = CGRect(x:(rect.size.width - 220) / 2, y: 65, width: 16, height: 2)
    line_1.path  = UIBezierPath(roundedRect: recttemp1
        , cornerRadius: 0).CGPath
    
    var recttemp2:CGRect = CGRect(x:(rect.size.width - 220) / 2, y: 65, width: 2, height: 16)
    line_2.path  = UIBezierPath(roundedRect: recttemp2
        , cornerRadius: 0).CGPath
    
    
    
    
    var recttemp3:CGRect = CGRect(x:(rect.size.width - 220) / 2, y: 65+220-16, width: 2, height: 16)
    line_3.path  = UIBezierPath(roundedRect: recttemp3
        , cornerRadius: 0).CGPath
    
    var recttemp4:CGRect = CGRect(x:(rect.size.width - 220) / 2, y: 65+220-2, width: 16, height: 2)
    line_4.path  = UIBezierPath(roundedRect: recttemp4
        , cornerRadius: 0).CGPath
    
    
    
    
    
    var recttemp5:CGRect = CGRect(x:(rect.size.width - 220) / 2  + 220 - 16, y: 65, width: 16, height: 2)
    line_5.path  = UIBezierPath(roundedRect: recttemp5
        , cornerRadius: 0).CGPath
    
    var recttemp6:CGRect = CGRect(x:(rect.size.width - 220) / 2 + 220 - 2, y: 65, width: 2, height: 16)
    line_6.path  = UIBezierPath(roundedRect: recttemp6
        , cornerRadius: 0).CGPath
    
    
    
    
    var recttemp7:CGRect = CGRect(x:(rect.size.width - 220) / 2 + 220 - 2, y: 65+220-16, width: 2, height: 16)
    line_7.path  = UIBezierPath(roundedRect: recttemp7
        , cornerRadius: 0).CGPath
    
    var recttemp8:CGRect = CGRect(x:(rect.size.width - 220) / 2 + 220 - 16, y: 65+220-2, width: 16, height: 2)
    line_8.path  = UIBezierPath(roundedRect: recttemp8
        , cornerRadius: 0).CGPath
    
    

    

    
    gradientLayer = CAGradientLayer(layer: topview.layer)
    gradientLayer?.frame = CGRect(x:(rect.size.width - 220) / 2, y: 65, width: 220, height: 2 )

    gradientLayer?.colors = [UIColor(red: 43/255.0, green: 205/255.0, blue: 156/255.0, alpha: 0).CGColor,UIColor(red: 43/255.0, green: 205/255.0, blue: 156/255.0, alpha: 1).CGColor,UIColor(red: 43/255.0, green: 205/255.0, blue: 156/255.0, alpha: 0).CGColor  ]
    
    gradientLayer?.locations = [0,0.5,1.0];

    gradientLayer?.startPoint = CGPointMake(0, 0);
    gradientLayer?.endPoint = CGPointMake(1, 0);
   

    
    topview.layer.addSublayer(gradientLayer)
    
    
   
    addtoView()
    
    
    lexingcodeButton = CommonUtil.buttonSetImage(lexingcodeButton, name: "remove_devicebtn", states: [UIControlState.Normal,UIControlState.Selected,UIControlState.Highlighted])
    lexingcodeButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
    lexingcodeButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
    
    self.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
    
    self.navigationItem.title = "连接乐行宝设备"
    
    self.navigationController?.navigationBar.tintColor=UIColor.whiteColor()
    
    
}


func addtoView(){
    
    topview.layer.addSublayer(line_1)
    topview.layer.addSublayer(line_2)
    topview.layer.addSublayer(line_3)
    topview.layer.addSublayer(line_4)
    topview.layer.addSublayer(line_5)
    topview.layer.addSublayer(line_6)
    topview.layer.addSublayer(line_7)
    topview.layer.addSublayer(line_8)

}

var dotimeer = true
var _y:CGFloat = 0


var  duration:CFTimeInterval?;
var  timeOffset:CFTimeInterval?;
var  lastStep:CFTimeInterval?;




func chageline(link:CADisplayLink) -> Void {
    
    

    
    if(_y>=(220)){
        _y = 0
    }
    
   
    CATransaction.begin()
    CATransaction.setAnimationDuration(0)
     self.gradientLayer?.position.y =  65 + _y
    CATransaction.commit()
    
   

    
    var thisStep = CACurrentMediaTime()
    
    var stepDuration1 = thisStep - self.lastStep!;
    
    self.lastStep = thisStep;
    

     _y = _y + CGFloat(100 * (stepDuration1))
    
 
    
    
    
    
    
}


deinit {
    codeReader?.stopScanning()
    
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

override func viewDidAppear(animated: Bool) {
    
    
    

}


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    dotimeer = true
    looper?.invalidate()
    looper = CADisplayLink(target: self, selector: Selector("chageline:"))
    looper?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
    lastStep = CACurrentMediaTime()

    
    codeReader?.startScanning()
}

override func viewWillDisappear(animated: Bool) {
    codeReader?.stopScanning()
    
    dotimeer = false
    
    looper?.invalidate()
    
    super.viewWillDisappear(animated)
}



override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    
    codeReader?.previewLayer.frame = view.bounds
    
    self.view.bringSubviewToFront(topview)
    self.topview.bringSubviewToFront(self.textLable1)
    self.topview.bringSubviewToFront(self.textLable2)
    self.topview.bringSubviewToFront(self.lexingcodeButton)

}


// MARK: - Managing the Orientation

func orientationDidChanged(notification: NSNotification) {
    topview.setNeedsDisplay()
    
    if codeReader?.previewLayer.connection != nil {
        let orientation = UIApplication.sharedApplication().statusBarOrientation
        
        codeReader?.previewLayer.connection.videoOrientation = QRCodeReader.videoOrientationFromInterfaceOrientation(orientation)
    }
}

// MARK: - Initializing the AV Components

private func setupUIComponentsWithCancelButtonTitle() {


}

private func setupAutoLayoutConstraints() {
    let views: [NSObject: AnyObject] = ["cameraView": topview]
    
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[cameraView]|", options: .allZeros, metrics: nil, views: views))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[cameraView]|", options: .allZeros, metrics: nil, views: views))

}}

效果: 二维码扫描


中间的扫描线不能用NSTimer做,,需采用CADisplayLink 即在界面渲染帧前做 这样才不会出现卡顿感(位置计算不要过于复杂,,会影响帧率)

© 著作权归作者所有

温红权
粉丝 0
博文 12
码字总数 2059
作品 0
南京
私信 提问
EyreFree/EFQRCode

EFQRCode 是一个轻量级的、用来生成和识别二维码的纯 Swift 库,可根据输入的水印图和图标产生艺术二维码,基于 CoreImage 进行开发。受 qrcode 启发。EFQRCode 为你提供了一种更好的在你的 ...

EyreFree
2017/04/06
0
0
【微信赠书活动】《Swift权威指南 》随书配有教学视频哟!!

【微信赠书活动】今日将赠出《Swift权威指南 》5本。 参与方法:第一步:扫描下方二维码 第二步扫描后关注我们的微信公众账号 3.回复书名 完成这三步即可参与。活动截止至明天晚上10:00 。到...

生气的散人
2014/09/04
628
10
Swift_iOS: 扫描二维码的方法

可以使用AVFoundation框架来启动相机扫描二维码,把一个二维码转换为一个字符串。 如下应用,进入首页看到一个按钮和一个标签。点按钮的话,会触发一次扫描,把扫描到的二维码转换为字符串后...

刘传君
2017/09/01
0
0
swift手撕二维码

超市付款扫一扫,免费wifi扫一扫,添加好友扫一扫。 二维码就像是神一般的存在!! 可是到底二维码是个啥呢? QRCode.jpg 一、简介 1、概念 用某种特定的几何图形按照一定规律在平面分布的黑...

阡陌有客
2017/11/27
0
0
一句话搞定webmap(一)——轻地图组件

摘要: 遥想当年,在APP中加入LBS元素相当困难:要刻苦学习java,要刻苦学习iOS开发,要刻苦学习javascript…… 而如今,要制作一张地图真是越来越容易了!竟然只需要一句话,就可以打点,导...

酸奶小妹GIS
2014/08/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周日乱弹 —— 程序员做噩梦

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @-冰冰棒- :#今日歌曲推荐# 手嶌葵《Kiss The Girl》 《Kiss The Girl》- 手嶌葵 手机党少年们想听歌,请使劲儿戳(这里) @Sharon啊 :今天...

小小编辑
28分钟前
62
3
Another app is currently holding the yum lock; waiting for it to exit...

Another app is currently holding the yum lock; waiting for it to exit... The other application is: PackageKit Memory : 153 M RSS (266 MB VSZ) Started: Thu Jul 12 00:03......

圣洁之子
36分钟前
0
0
FastDateFormat 研究

FastDateFormat 对缓存的利用,其实就是用ConcurrentHashMap 做了一个map类型的缓存 public F getInstance(final String pattern, TimeZone timeZone, Locale locale) { Validate......

暗中观察
今天
3
0
Android双向绑定原理简述

Android双向绑定原理简述 双向绑定涉及两个部分,即将业务状态的变化传递给UI,以及将用户输入信息传递给业务模型。 首先我们来看业务状态是如何传递给UI的。开启dataBinding后,编译器为布局...

tommwq
今天
4
0
Spring系列教程八: Spring实现事务的两种方式

一、 Spring事务概念: 事务是一系列的动作,它们综合在一起才是一个完整的工作单元,这些动作必须全部完成,如果有一个失败的话,那么事务就会回滚到最开始的状态,仿佛什么都没发生过一样。...

我叫小糖主
今天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部