本文主要是介绍利用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实现简单坦克移动操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!