iOS游戏控制手柄

本文介绍了如何在iOS平台上实现游戏控制手柄的功能,包括DirectionControl的头文件和实现文件的详细内容,帮助开发者理解并构建游戏操控体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值