扫描区域是根据rectOfInterest属性设置的,但是这个属性里的参数是非常恶心的,因为CGRect中的x,y进行了调换,width和height进行了调换,而且参数必须是比例
let kMargin = 40
let scanViewW = UIScreen.main.bounds.width - CGFloat(kMargin * 2)
let scanViewH = UIScreen.main.bounds.width - CGFloat(kMargin * 2)
self.metadataOutput.rectOfInterest = CGRect(
x: ((UIScreen.main.bounds.height - scanViewW) / 2 ) / UIScreen.main.bounds.height ,//这是y对应的比例
y: (kMargin) / UIScreen.main.bounds.width, //这是x对应的比例
width: scanViewW / UIScreen.main.bounds.height, //这是height对应的比例
height: scanViewH / UIScreen.main.bounds.width //这是width对应的比例
)