利用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#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Java利用Spire.Doc for Java实现在模板的基础上创建Word文档

《Java利用Spire.DocforJava实现在模板的基础上创建Word文档》在日常开发中,我们经常需要根据特定数据动态生成Word文档,本文将深入探讨如何利用强大的Java库Spire.Do... 目录1. Spire.Doc for Java 库介绍与安装特点与优势Maven 依赖配置2. 通过替换