利用UImageView实现简单坦克移动操作

2024-06-11 00:08

本文主要是介绍利用UImageView实现简单坦克移动操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要需要使用的一些类:UIImageView,NSTimer。


需要注意的是

touchDown 代表按下去,touch upOutside代表离开按下去的区域都应指向所要触发的button


mainViewController.h

#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController

{

    UIImageView *tankImage;

    NSTimer *timerXia, *timerShang, *timerZuo, *timerYou;

    int x,y;

}

@property(retain,nonatomic) UIImageView *tankImage;

@property(retain,nonatomic) NSTimer *timerXia, *timerShang, *timerZuo, *timerYou;

@property int x,y;

-(IBAction)shang:(id)sender;

-(IBAction)xia:(id)sender;

-(IBAction)zuo:(id)sender;

-(IBAction)you:(id)sender;

-(IBAction)zuoOut:(id)sender;

-(IBAction)youOut:(id)sender;

-(IBAction)shangOut:(id)sender;

@end


MainViewController.m


#import "MainViewController.h"


@interface MainViewController ()


@end


@implementation MainViewController

@synthesize tankImage;

@synthesize timerXia,timerShang,timerZuo,timerYou;

@synthesize x,y; 

- (void)dealloc

{

    [timerXia release];

    [timerShang release];

    [timerZuo release];

    [timerYou release];

    [tankImage release];

    [super dealloc];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    UIImageView *iv;

    UIImage *ig;

    ig = [UIImage imageNamed:@"p1tank1D.gif"];

    iv = [[[UIImageView alloc]initWithImage:ig]autorelease];

    iv.frame = CGRectMake(0, 0, 50, 50);

    tankImage = iv;

    [self.view addSubview:tankImage];

   

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(IBAction)shang:(id)sender

{

    timerShang= [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(shang:) userInfo:nil repeats:NO];

    if (y == 0)

    {

        y = 0;

    }

    else

    y=y-10;

    UIImage *ig;

    ig = [UIImage imageNamed:@"p1tank1U.gif"];

    tankImage.image = ig;

    tankImage.frame =CGRectMake(x, y, 50, 50);

}

-(IBAction)shangOut:(id)sender

{

    [timerShang invalidate];

}

-(IBAction)xia:(id)sender

{

    

    timerXia= [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(xia:) userInfo:nil repeats:NO];

    if (y >= 480-50)

    {

        y = 480-50;

    }

    else

    {

        y= y+10;

    }

    UIImage *ig;

    ig = [UIImage imageNamed:@"p1tank1D.gif"];

    tankImage.image = ig;

    tankImage.frame =CGRectMake(x, y, 50, 50);

    

   

    

}

-(IBAction)xiaOut:(id)sender

{

     [timerXia invalidate];

}

-(IBAction)zuo:(id)sender

{

    timerZuo= [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(zuo:) userInfo:nil repeats:NO];

    if (x == 0)

    {

        x = 0;

    }

    else

        x=x-10;

    UIImage *ig;

    ig = [UIImage imageNamed:@"p1tank1L.gif"];

    tankImage.image = ig;

    tankImage.frame =CGRectMake(x, y, 50, 50);

}

-(IBAction)zuoOut:(id)sender

{

    [timerZuo invalidate];

}

-(IBAction)you:(id)sender

{

     timerYou= [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(you:) userInfo:nil repeats:NO];

    if (x >= 320-50) {

        x = 320-50;

    }

    else

    {

        x= x+10;

    }

    UIImage *ig;

    ig = [UIImage imageNamed:@"p1tank1R.gif"];

    tankImage.image = ig;

    tankImage.frame =CGRectMake(x, y, 50, 50);

}

-(IBAction)youOut:(id)sender

{

    [timerYou invalidate];

}


@end


http://pan.baidu.com/share/link?shareid=3807969651&uk=1863550082 附上一个DEMO 



这篇关于利用UImageView实现简单坦克移动操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1049651

相关文章

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

一份LLM资源清单围观技术大佬的日常;手把手教你在美国搭建「百万卡」AI数据中心;为啥大模型做不好简单的数学计算? | ShowMeAI日报

👀日报&周刊合集 | 🎡ShowMeAI官网 | 🧡 点赞关注评论拜托啦! 1. 为啥大模型做不好简单的数学计算?从大模型高考数学成绩不及格说起 司南评测体系 OpenCompass 选取 7 个大模型 (6 个开源模型+ GPT-4o),组织参与了 2024 年高考「新课标I卷」的语文、数学、英语考试,然后由经验丰富的判卷老师评判得分。 结果如上图所

通过SSH隧道实现通过远程服务器上外网

搭建隧道 autossh -M 0 -f -D 1080 -C -N user1@remotehost##验证隧道是否生效,查看1080端口是否启动netstat -tuln | grep 1080## 测试ssh 隧道是否生效curl -x socks5h://127.0.0.1:1080 -I http://www.github.com 将autossh 设置为服务,隧道开机启动

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现

回调的简单理解

之前一直不太明白回调的用法,现在简单的理解下 就按这张slidingmenu来说,主界面为Activity界面,而旁边的菜单为fragment界面。1.现在通过主界面的slidingmenu按钮来点开旁边的菜单功能并且选中”区县“选项(到这里就可以理解为A类调用B类里面的c方法)。2.通过触发“区县”的选项使得主界面跳转到“区县”相关的新闻列表界面中(到这里就可以理解为B类调用A类中的d方法

基于Springboot + vue 的抗疫物质管理系统的设计与实现

目录 📚 前言 📑摘要 📑系统流程 📚 系统架构设计 📚 数据库设计 📚 系统功能的具体实现    💬 系统登录注册 系统登录 登录界面   用户添加  💬 抗疫列表展示模块     区域信息管理 添加物资详情 抗疫物资列表展示 抗疫物资申请 抗疫物资审核 ✒️ 源码实现 💖 源码获取 😁 联系方式 📚 前言 📑博客主页:

移动硬盘盒:便携与交互的完美结合 PD 充电IC

在数字化时代的浪潮中,数据已成为我们生活中不可或缺的一部分。随着数据的不断增长,人们对于数据存储的需求也在不断增加。传统的存储设备如U盘、光盘等,虽然具有一定的便携性,但在容量和稳定性方面往往难以满足现代人的需求。而移动硬盘,以其大容量、高稳定性和可移动性,成为了数据存储的优选方案。然而,单纯的移动硬盘在携带和使用上仍存在诸多不便,于是,移动硬盘盒应运而生,以其独特的便携性和交互性,成为了数据存储