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

二维码扫描:

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 即在界面渲染帧前做 这样才不会出现卡顿感(位置计算不要过于复杂,,会影响帧率)

转载于:https://my.oschina.net/u/922864/blog/408024

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值