IOS开发小功能2:二维码扫描界面的设计(横线上下移动)



效果图如上,实现的是一个二维码扫描界面。
下面我贴出线条上下移动的代码,至于二维码的代码是用的第三方库。
首先是整体的结构:

注意下面的库文件一个都不能少,否则会报错。
TLTiltHighlightView是划线的类。
#import <QuartzCore/QuartzCore.h>
#import <CoreMotion/CoreMotion.h> #import "TLTiltHighlightView.h" // Private properties.
@interface TLTiltHighlightView () // Our gradient layer.
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
// Our motion manager.
@property (nonatomic, strong) CMMotionManager *motionManager; @end @implementation TLTiltHighlightView #pragma mark - Public Initializers // Allows support for using instances loaded from nibs or storyboards.
-(id)initWithCoder:(NSCoder *)aCoder
{
if (!(self = [super initWithCoder:aCoder])) return nil; [self setup]; return self;
} // Allows support for using instances instantiated programatically.
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame])) return nil; [self setup]; return self;
} // We need to stop our motionManager from continuing to update once our instance is deallocated.
-(void)dealloc
{
[self.motionManager stopAccelerometerUpdates];
} #pragma mark - Private methods // Sets up the initial state of the view.
-(void)setup
{
// Set up the gradient
[self setupGradient];
// Set up our motion updates
[self setupMotionDetection];
} // Creates the gradient and sets up some default properties
-(void)setupGradient
{
NSAssert(self.gradientLayer == nil, @"Gradient layer being set up more than once."); self.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.18f];
self.highlightColor = [UIColor colorWithWhite:1.0f alpha:0.36f]; // Create a new, clear gradient layer and add it to our layer hierarchy.
self.gradientLayer = [CAGradientLayer layer];
[self.layer addSublayer:self.gradientLayer];
self.gradientLayer.backgroundColor = [[UIColor clearColor] CGColor];
// Make the layer gradient horizontal
self.gradientLayer.startPoint = CGPointMake(, 0.5);
self.gradientLayer.endPoint = CGPointMake(, 0.5); // Set up the colours and position
[self updateGradientColours];
[self updateGradientLayerPosition];
} // Starts the
-(void)setupMotionDetection
{
NSAssert(self.motionManager == nil, @"Motion manager being set up more than once."); // Set up a motion manager and start motion updates, calling deviceMotionDidUpdate: when updated.
self.motionManager = [[CMMotionManager alloc] init]; __weak __typeof(self) weakSelf = self;
[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
if (error)
{
[weakSelf.motionManager stopDeviceMotionUpdates];
return;
} [weakSelf deviceMotionDidUpdate:motion];
}];
} // Updates the gradient layer to fill our bounds.
-(void)updateGradientLayerPosition
{
if ([[UIScreen mainScreen] scale] > )
{
// Running on a retina device
// self.gradientLayer.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - 1.5f, CGRectGetWidth(self.bounds), 1.5f);
self.gradientLayer.frame = CGRectMake(, CGRectGetHeight(self.bounds) - 1.5f, CGRectGetWidth(self.bounds), );
}
else
{
// Running on a non-Retina device
// self.gradientLayer.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - 1, CGRectGetWidth(self.bounds), 1);
self.gradientLayer.frame = CGRectMake(, CGRectGetHeight(self.bounds) - 1.5f, CGRectGetWidth(self.bounds), ); }
} // Updates the gradient's colours.
-(void)updateGradientColours
{
self.gradientLayer.colors = @[(id)[[UIColor clearColor] CGColor],
(id)[self.highlightColor CGColor],
(id)[[UIColor clearColor] CGColor]];
} #pragma mark CoreMotion Methods -(void)deviceMotionDidUpdate:(CMDeviceMotion *)deviceMotion
{
// Called when the deviceMotion property of our CMMotionManger updates.
// Recalculates the gradient locations. // Ration from the center which the centre of the gradient is permitted to move.
// (ie: the centre of the gradient may be 1/4 the distance of the view from the centre.)
const CGFloat maxDistanceRatioFromCenter = 4.0f; // We need to account for the interface's orientation when calculating the relative roll.
CGFloat roll = 0.0f;
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationPortrait:
roll = deviceMotion.attitude.roll;
break;
case UIInterfaceOrientationPortraitUpsideDown:
roll = -deviceMotion.attitude.roll;
break;
case UIInterfaceOrientationLandscapeLeft:
roll = -deviceMotion.attitude.pitch;
break;
case UIInterfaceOrientationLandscapeRight:
roll = deviceMotion.attitude.pitch;
break;
} // This will give us an interpolated value [-0.4 ... 0.4].
CGFloat interpolatedValue = sinf(roll) / maxDistanceRatioFromCenter; // We need to convert our ration to a decimal (0.4, in this case).
CGFloat maxDistanceDecimalFromCenter = maxDistanceRatioFromCenter / 10.0f; // Find the middle position for our gradient. This needs to be in the range of [0 ... 1].
CGFloat gradientMiddlePosition = (interpolatedValue + maxDistanceDecimalFromCenter) / (maxDistanceDecimalFromCenter * 2.0f); // Finally, update our gradient layer.
self.gradientLayer.locations = @[@(), @(gradientMiddlePosition), @()];
} #pragma mark Overridden Methods -(void)setFrame:(CGRect)frame
{
[super setFrame:frame]; [self updateGradientLayerPosition];
} -(void)setBounds:(CGRect)bounds
{
[super setBounds:bounds]; [self updateGradientLayerPosition];
} #pragma mark - Overridden Properties -(void)setHighlightColor:(UIColor *)highlightColor
{
_highlightColor = highlightColor; [self updateGradientColours];
} @end
#import "ViewController.h"
#import "TLTiltHighlightView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIImageView *hbImageview=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"saomiao.png"]] autorelease];
CGRect hbImagerect=CGRectMake(, , , );
[hbImageview setFrame:hbImagerect];
[self.view addSubview:hbImageview]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)saomiao:(id)sender { //上下扫描线
TLTiltHighlightView *highlightView = [[TLTiltHighlightView alloc] initWithFrame:CGRectMake(, , , )];
highlightView.highlightColor = [UIColor greenColor];
highlightView.backgroundColor = [UIColor clearColor];
highlightView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:highlightView];
while () {
[ViewController moveDown:highlightView andAnimationDuration:2.0 andWait:YES andLength:90.0];
[ViewController moveUp:highlightView andAnimationDuration:2.0 andWait:YES andLength:90.0];
} } + (void) moveUp: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length
{
__block BOOL done = wait; //wait = YES wait to finish animation
[UIView animateWithDuration:duration animations:^{
view.center = CGPointMake(view.center.x, view.center.y-length); } completion:^(BOOL finished) {
done = NO;
}];
// wait for animation to finish
while (done == YES)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
} + (void) moveDown: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
__block BOOL done = wait; //wait = YES wait to finish animation
[UIView animateWithDuration:duration animations:^{
view.center = CGPointMake(view.center.x, view.center.y + length); } completion:^(BOOL finished) {
done = NO;
}];
// wait for animation to finish
while (done == YES)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
} @end
然后是ViewController
主要实现的是两个类方法,实现上下移动。
+ (void) moveUp: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length
{
__block BOOL done = wait; //wait = YES wait to finish animation
[UIView animateWithDuration:duration animations:^{
view.center = CGPointMake(view.center.x, view.center.y-length); } completion:^(BOOL finished) {
done = NO;
}];
// wait for animation to finish
while (done == YES)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
} + (void) moveDown: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
__block BOOL done = wait; //wait = YES wait to finish animation
[UIView animateWithDuration:duration animations:^{
view.center = CGPointMake(view.center.x, view.center.y + length); } completion:^(BOOL finished) {
done = NO;
}];
// wait for animation to finish
while (done == YES)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
IOS开发小功能2:二维码扫描界面的设计(横线上下移动)的更多相关文章
- iOS中 基于LBXScan库二维码扫描 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后 ...
- IOS开发技巧快速生成二维码
随着移动互联网的发展,二维码应用非常普遍,各大商场,饭店,水果店 基本都有二维码的身影,那么ios中怎么生成二维码呢? 下面的的程序演示了快速生成二维码的方法: 在ios里面要生成二维码,需要借助一个 ...
- iOS开发——高级技术&生成二维码
生成二维码 因为项目里需要新增个功能,该功能用到了二维码技术.于是我便查阅了资料,先学习了二维码的生成. 我们使用libqrencode库来生成二维码.下载地址http://download.cs ...
- iOS开发——生成条形码,二维码
- (void)viewDidLoad { [super viewDidLoad]; self.imageView.image = [self generateBarCode:@"15248 ...
- iOS 简单易用的二维码扫描及生成二维码三方控件LFQRCode,可灵活自定义UI
一.扫码 扫描的控件是一个view,使用者只需贴在自己的控制器内即可.其他UI用户可在自己控制器随便添加.代码如下 - (void)viewDidLoad { [super viewDidLoad]; ...
- iOS 原生库(AVFoundation)实现二维码扫描,封装的工具类,不依赖第三方库,可高度自定义扫描动画及界面(Swift 4.0)
Create QRScanner.swift file // // QRScanner.swift // NativeQR // // Created by Harvey on 2017/10/24. ...
- 自定义ZXing二维码扫描界面并解决取景框拉伸等问题
先看效果 扫描内容是下面这张,二维码是用zxing库生成的 由于改了好几个类,还是去年的事都忘得差不多了,所以只能上这个类的代码了,主要就是改了这个CaptureActivity.java packa ...
- 二维码框架ZBarSDK的使用和自己定义二维码扫描界面方法
假设你不知道ZBarSDK怎么用,请下载demo http://download.csdn.net/detail/u013686641/7858917 假设你已经配置好ZBarSDK .那么以下这个类 ...
- react-native 扫一扫功能(二维码扫描)功能开发
1.安装插件 yarn add react-native-smart-barcode 2.关联 react-native link react-native-smart-barcode 3.修改 an ...
随机推荐
- VC中MessageBox的常见用法
一.关于MessageBox 消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.MessageBox("这是一个最简单的 ...
- js isArray小结
原文:[转载]js isArray小结 在日常开发中,我们经常需要判断某个对象是否是数组类型的,在js中检测对象类型的常见的方法有几种: 1.typeof操作符.对于Function.String.N ...
- Tair LDB基于Prefixkey中期范围内查找性能优化项目总结
"Tair LDB基于Prefixkey该范围内查找性能优化"该项目是仅一个月.这个月主要是熟悉项目..以下从几个方面总结下个人在该项目上所做的工作及自己的个人所得所感. 项目工作 ...
- Cocos发育Visual Studio下一个HttpClient开发环境设置
Cocos2d-x 3.x相关类集成到网络通信libNetwork图书馆project于.这其中包括:HttpClient分类. 我们需要在Visual Studio溶液中加入libNetwork图书 ...
- uva 12003 Array Transformer (大规模阵列)
白皮书393页面. 乱搞了原始数组中.其实用另一种阵列块记录. 你不能改变原始数组. 请注意,与原来的阵列和阵列块的良好关系,稍微细心处理边境.这是不难. #include <cstdio> ...
- HDU 1243 畅通project 并査集
Total Submission(s): 31033 Accepted Submission(s): 16313 Problem Description 某省调查城镇交通状况,得到现有城镇道路统 ...
- 2014鞍山直播比赛H称号HDU5077(DFS修剪+通过计)
NAND Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Sub ...
- 分布式服务弹性框架“Hystrix”实践与源码研究(一)
文章初衷 为了应对将来在线(特别是无线端)业务量的成倍增长,后端服务的分布式化程度需要不断提高,对于服务的延迟和容错管理将面临更大挑战,公司框架和开源团队选择内部推广Netflix的Hystrix,一 ...
- 网站静态化处理—CSI(5)
网站静态化处理—CSI(5) 讲完了SSI,ESI,下面就要讲讲CSI了 ,CSI是浏览器端的动静整合方案,当我文章发表后有朋友就问我,CSI技术是不是就是通过ajax来加载数据啊,我当时的回答只是说 ...
- SQL点滴7—使用SQL Server的attach功能出现错误及解决方法
原文:SQL点滴7-使用SQL Server的attach功能出现错误及解决方法 今天用SQL Server 2008的attach功能附加一个数据库,出了点问题,提示的错误是: Unable to ...