效果图如上,实现的是一个二维码扫描界面。

下面我贴出线条上下移动的代码,至于二维码的代码是用的第三方库。

首先是整体的结构:

注意下面的库文件一个都不能少,否则会报错。

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]];
}

代码下载

最新文章

  1. Bean的生命周期
  2. CSS 选择器之基本选择器 属性选择器 伪类选择器
  3. Spring mvc 转发、重定向
  4. Func和Action委托简单用法
  5. JavaScript设计模式(4)-桥接模式
  6. spring @CrossOrigin解决跨域问题
  7. Cocos2dx制作帧动画
  8. Hash 1.04 右键
  9. linux内核编程helloworld(中级)
  10. 37.Linux驱动调试-根据oops的栈信息,确定函数调用过程
  11. 初识Nosql
  12. 20145331魏澍琛《网络对抗》Exp5 MSF基础应用
  13. Weekly Contest 132
  14. flask插件系列之flask_uploads上传文件
  15. cocos2d0基础篇笔记一
  16. sql语句-8-sql学习流程
  17. vue-cli项目启动遇到的坑
  18. Oracle 数据库异机恢复(归档模式)
  19. $Android AlertDialog的各种用法总结
  20. HANA 与BW

热门文章

  1. 【iOS开展-94】xcode6如何使用GIT以及如何添加太老项目GIT特征?
  2. Win7安装和配置Tigase 5.2server
  3. JAVA设计模式(09):结构化-代理模式(Proxy)
  4. HDU 3376 &amp;amp;&amp;amp; 2686 方格取数 最大和 费用流裸题
  5. HDU 4793 2013 Changsha Regional Collision[简单的平面几何]
  6. HTML的标签canvas
  7. The Swift Programming Language-官方教程精译Swift(8)闭包 -- Closures
  8. 苹果新的编程语言 Swift 语言进阶(十二)--选项链
  9. UVA 11525 Permutation(树状数组)
  10. 12个有趣的c面试题目