使用加速计用作方向控制器滚动图片(弹珠)
2016-05-20 11:13
435 查看
BIDBallView.h
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
@interface BIDBallView : UIView
@property (nonatomic,assign) CMAcceleration acceleration;
@end
BIDBallView.m
#import "BIDBallView.h"
@interface BIDBallView ()
@property (strong,nonatomic) UIImage *image;
@property (assign,nonatomic) CGPoint currentPoint;
@property (assign,nonatomic) CGPoint previousPoint;
@property (assign,nonatomic) CGFloat ballXVelocity; //Xzhou上的速度
@property (assign,nonatomic) CGFloat ballYVelocity; //Yzhou上的速度
@end
@implementation BIDBallView
-(void)commonInit{
self.image = [UIImage imageNamed:@"pic_客服头像@2x.png"];
self.currentPoint = CGPointMake((self.bounds.size.width /2.0f)+(self.image.size.width/2.0f), (self.bounds.size.height/2.0f)+(self.bounds.size.height/2.0f));
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
-(void)drawRect:(CGRect)rect{
[self.image drawAtPoint:self.currentPoint]; //绘图代码
}
#pragma Mark
-(void)setCurrentPoint:(CGPoint)newPoint{
self.previousPoint = self.currentPoint; //旧的值存在previousPoint
_currentPoint = newPoint; //新值赋给currentPoint
if (self.currentPoint.x < 0) {
_currentPoint.x = 0;
self.ballXVelocity = 0;
}
if (self.currentPoint.y < 0) {
_currentPoint.y = 0;
self.ballYVelocity = 0;
}
if (self.currentPoint.x > self.bounds.size.width - self.image.size.width) {
_currentPoint.x = self.bounds.size.width - self.image.size.width;
self.ballXVelocity = 0;
}
if (self.currentPoint.y > self.bounds.size.width - self.image.size.width ){
_currentPoint.y = self.bounds.size.width - self.image.size.width;
self.ballYVelocity = 0;
}
CGRect currentRect = CGRectMake(self.currentPoint.x, self.currentPoint.y, self.currentPoint.x+self.image.size.width, self.currentPoint.y+self.image.size.height);
CGRect previousRect = CGRectMake(self.previousPoint.x, self.previousPoint.y, self.previousPoint.x+self.image.size.width, self.previousPoint.y+self.image.size.height);
[self setNeedsDisplayInRect:CGRectUnion(currentRect, previousRect)];
}
-(void)update{
static NSDate *lastUpdateTime = nil;
if (lastUpdateTime != nil) {
NSTimeInterval secondSinceLastDraw = [[NSDate date]timeIntervalSinceDate:lastUpdateTime];
self.ballYVelocity = self.ballYVelocity - (self.acceleration.y * secondSinceLastDraw);
self.ballXVelocity = self.ballXVelocity + (self.acceleration.x *secondSinceLastDraw);
CGFloat xAccel = secondSinceLastDraw * self.ballXVelocity *500;
CGFloat yAccel = secondSinceLastDraw *self.ballYVelocity *500;
self.currentPoint = CGPointMake(self.currentPoint.x + xAccel, self.currentPoint.y + yAccel);
}
lastUpdateTime = [[NSDate alloc]init]; //用当前时间更新最后时间
}
@end
ViewController.m
#import "ViewController.h"
#import "BIDBallView.h"
#import <CoreMotion/CoreMotion.h>
#define kUpdateInterval (1.0f / 60.0f)
@interface ViewController ()
@property (nonatomic,strong)CMMotionManager *motionManager;
@property (nonatomic,strong)NSOperationQueue *queue;
@end
@implementation ViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc]init];
self.queue = [[NSOperationQueue alloc]init];
self.motionManager.accelerometerUpdateInterval = kUpdateInterval;
[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
[(id)self.view setAcceleration:accelerometerData.acceleration];
[self.view performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
}];
}
@end
有需要的可以参考我的github:https://github.com/linxiu/ERBall
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
@interface BIDBallView : UIView
@property (nonatomic,assign) CMAcceleration acceleration;
@end
BIDBallView.m
#import "BIDBallView.h"
@interface BIDBallView ()
@property (strong,nonatomic) UIImage *image;
@property (assign,nonatomic) CGPoint currentPoint;
@property (assign,nonatomic) CGPoint previousPoint;
@property (assign,nonatomic) CGFloat ballXVelocity; //Xzhou上的速度
@property (assign,nonatomic) CGFloat ballYVelocity; //Yzhou上的速度
@end
@implementation BIDBallView
-(void)commonInit{
self.image = [UIImage imageNamed:@"pic_客服头像@2x.png"];
self.currentPoint = CGPointMake((self.bounds.size.width /2.0f)+(self.image.size.width/2.0f), (self.bounds.size.height/2.0f)+(self.bounds.size.height/2.0f));
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
-(void)drawRect:(CGRect)rect{
[self.image drawAtPoint:self.currentPoint]; //绘图代码
}
#pragma Mark
-(void)setCurrentPoint:(CGPoint)newPoint{
self.previousPoint = self.currentPoint; //旧的值存在previousPoint
_currentPoint = newPoint; //新值赋给currentPoint
if (self.currentPoint.x < 0) {
_currentPoint.x = 0;
self.ballXVelocity = 0;
}
if (self.currentPoint.y < 0) {
_currentPoint.y = 0;
self.ballYVelocity = 0;
}
if (self.currentPoint.x > self.bounds.size.width - self.image.size.width) {
_currentPoint.x = self.bounds.size.width - self.image.size.width;
self.ballXVelocity = 0;
}
if (self.currentPoint.y > self.bounds.size.width - self.image.size.width ){
_currentPoint.y = self.bounds.size.width - self.image.size.width;
self.ballYVelocity = 0;
}
CGRect currentRect = CGRectMake(self.currentPoint.x, self.currentPoint.y, self.currentPoint.x+self.image.size.width, self.currentPoint.y+self.image.size.height);
CGRect previousRect = CGRectMake(self.previousPoint.x, self.previousPoint.y, self.previousPoint.x+self.image.size.width, self.previousPoint.y+self.image.size.height);
[self setNeedsDisplayInRect:CGRectUnion(currentRect, previousRect)];
}
-(void)update{
static NSDate *lastUpdateTime = nil;
if (lastUpdateTime != nil) {
NSTimeInterval secondSinceLastDraw = [[NSDate date]timeIntervalSinceDate:lastUpdateTime];
self.ballYVelocity = self.ballYVelocity - (self.acceleration.y * secondSinceLastDraw);
self.ballXVelocity = self.ballXVelocity + (self.acceleration.x *secondSinceLastDraw);
CGFloat xAccel = secondSinceLastDraw * self.ballXVelocity *500;
CGFloat yAccel = secondSinceLastDraw *self.ballYVelocity *500;
self.currentPoint = CGPointMake(self.currentPoint.x + xAccel, self.currentPoint.y + yAccel);
}
lastUpdateTime = [[NSDate alloc]init]; //用当前时间更新最后时间
}
@end
ViewController.m
#import "ViewController.h"
#import "BIDBallView.h"
#import <CoreMotion/CoreMotion.h>
#define kUpdateInterval (1.0f / 60.0f)
@interface ViewController ()
@property (nonatomic,strong)CMMotionManager *motionManager;
@property (nonatomic,strong)NSOperationQueue *queue;
@end
@implementation ViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc]init];
self.queue = [[NSOperationQueue alloc]init];
self.motionManager.accelerometerUpdateInterval = kUpdateInterval;
[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
[(id)self.view setAcceleration:accelerometerData.acceleration];
[self.view performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
}];
}
@end
有需要的可以参考我的github:https://github.com/linxiu/ERBall
相关文章推荐
- 几种常见代码管理工具比较(2009)
- c++第6次实验
- iOS开发 - 技巧 - 01 - 图片拉伸方式
- nslookup使用详解
- 怎么删除PDF文件的页面
- Android网络编程——https 不验证证书方式(信任所有证书)
- PAT 1033旧键盘打字 16分
- Java位运算总结:位运算用途广泛
- 同一个tomcat下面部署了两个项目,有一个访问不了404,web项目配置webAppRootKey
- Linux 文件乱码的解决方法
- 设计模式之命令模式php示例
- php登录利用$token验证
- VMware中linux硬盘空间不足的解决方法
- 短信发送api示例
- java如何设置Date类型的日期的年月日?
- 同域名下不同项目cookies取值问题
- 如何通过组策略修改IE的安全级别,记录帐户密码,实现moss的单点登陆
- 奇怪的背包题目 - Proud Merchants
- 2分钟读懂Hadoop和Spark的异同
- IAR编译器生成Cortex M3汇编代码的相关问题