<p style="margin-top: 0px; margin-bottom: 0px; font-size: 24px; line-height: normal; font-family: Menlo;"><pre name="code" class="objc">#import "ViewController.h"
#import "DirectionControl.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
DirectionControl *Controll = [[DirectionControl alloc] initWithFrame:CGRectMake(50, 100, 400, 400)];
[self.view addSubview:Controll];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
DirectionControl.h
#import <UIKit/UIKit.h>
#import <takeControlView/takeControlView.h>
@interface DirectionControl : UIView{
UIImageView *directionBg;
UIImageView *controlBg;
CGFloat SmallRockerCircleX,SmallRockerCircleY;
int R;
}
@end
DirectionControl.m
#import "DirectionControl.h"
@implementation DirectionControl
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self loadView];
// self.backgroundColor = [UIColor orangeColor];
}
return self;
}
-(void)loadView{
UIImage *image = nil;
image = [UIImage imageNamed:@"zhuan_2"];
directionBg = [[UIImageView alloc] initWithImage:image];
directionBg.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
// directionBg.center = CGPointMake(,);
directionBg.userInteractionEnabled = YES;
[self addSubview:directionBg];
//directionBg.backgroundColor = [UIColor redColor];
image = [UIImage imageNamed:@"qiu_2"];
controlBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
controlBg.center = CGPointMake(CGRectGetMidX(directionBg.bounds), CGRectGetMidY(directionBg.bounds));
[controlBg setImage:image];
//controlBg.backgroundColor = [UIColor greenColor];
controlBg.userInteractionEnabled = YES;
[directionBg addSubview:controlBg];
// motorHolder = [[SMotorHolder alloc]init];
UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction2:)];
[self addGestureRecognizer:pan2];
}
-(void)panAction2:(UIPanGestureRecognizer *)recognizer{
CGPoint p = [recognizer translationInView:controlBg];
CGPoint pp = [recognizer locationInView:self];
if (recognizer.state == UIGestureRecognizerStateBegan) {
controlBg.center = pp;//将中间控制球的位置移动到开始位置
}
CGPoint moveXY = CGPointMake(controlBg.center.x+p.x, controlBg.center.y+p.y);
if (moveXY.y<0) {
moveXY.y=0;
}
if (moveXY.x<0) {
moveXY.x=0;
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
//使小球随着手指运动
controlBg.center = [[takeControlView sharedInstance] controlForCircleView:recognizer centerXY:directionBg.center moveXY:moveXY fingerXY:pp R:70];
//防止移出屏幕
[recognizer setTranslation:CGPointZero inView:self];
}
// CGRectGetMidX得到一个fram中心店的X坐标
float x_offset =controlBg.center.x- CGRectGetMidX(directionBg.bounds) ;
float y_offset =CGRectGetMidY(directionBg.bounds)-controlBg.center.y ;
int xd= round(x_offset);
int yd= round(y_offset);
NSLog(@"%d, %d",xd,yd);
//将小球限制在圈内
if (xd<-100) {
xd = -100;
}
if (xd>100) {
xd = 100;
}
if (yd<-100) {
yd = -100;
}
if (yd>100) {
yd = 100;
}
//abs求绝对值
if(abs(xd)<30){
xd=0;
}
if (abs(yd)<30) {
yd=0;
}
NSLog(@"xd == %d, yd == %d",xd,yd);
[self sendCommandX:xd Y:yd];
if (recognizer.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:0.2 animations:^(void){
//松开手时,小球移动到中间位置
controlBg.center = CGPointMake(CGRectGetMidX(directionBg.bounds), CGRectGetMidY(directionBg.bounds));
}completion:^(BOOL finished){
for (int j = 0; j < 5; j ++) {
//发送停止命令
//[motorHolder stop];
}
num = 0;
//NSLog(@"stop!!!!");
}];
}
}
int num = 0;
-(void)sendCommandX:(int)x Y:(int)y{
if (ABS(x) > 25 && ABS(y) > 25) {
//第一象限
if (x > 0 && y > 0) {
if (num == 1) {
return;
}
num = 1;
NSLog(@"X == %d Y == %d",x,y);
NSLog(@"第一象限");
}
//第二象限
else if (x < 0 && y > 0){
if (num == 2) {
return;
}
num = 2;
NSLog(@"X == %d Y == %d",x,y);
NSLog(@"第二象限");
}
//第三象限
else if (x < 0 && y < 0){
if (num == 3) {
return;
}
num = 3;
NSLog(@"X == %d Y == %d",x,y);
NSLog(@"第三象限");
}
//第四象限
else if (x > 0 && y < 0){
if (num ==4) {
return;
}
NSLog(@"X == %d Y == %d",x,y);
num = 4;
NSLog(@"第四象限");
}
}
else{
//前进
if (abs(x) < 25 && y > 25) {
if (num == 5) {
return;
}
NSLog(@"X == %d Y == %d",x,y);
num = 5;
NSLog(@"前进");
}
//后退
else if (abs(x) < 25 && y < -25){
if (num == 6) {
return;
}
NSLog(@"X == %d Y == %d",x,y);
num = 6;
NSLog(@"后退");
}
//左转
else if (abs(y) < 25 && x < -25){
if (num == 7) {
return;
}
NSLog(@"X == %d Y == %d",x,y);
num = 7;
NSLog(@"左转");
}
//右转
else if (abs(y) < 25 && x > 25){
if (num == 8) {
return;
}
NSLog(@"X == %d Y == %d",x,y);
num = 8;
NSLog(@"右转");
}
}
}
@end
使用时需要导入:http://pan.baidu.com/s/1mha1dyg