iOS之简单的小车移动
2013-10-12 10:39
330 查看
这是一个简单的小程序,有上,下,左,右。四个按钮控制一个小车的移动。
ViewController.h文件
@interface ViewController :
UIViewController
{
UIButton * bt1;
UIButton * bt2;
UIButton *bt3;
UIButton *bt4;
UILabel * lab;
UIImageView *img;
}
@property (strong,
nonatomic) IBOutlet
UIButton * bt1;
@property (strong,
nonatomic) IBOutlet
UIButton * bt2;
@property (strong,
nonatomic) IBOutlet
UIButton * bt3;
@property (strong,
nonatomic) IBOutlet
UIButton * bt4;
@property (strong,
nonatomic) IBOutlet
UILabel * lab1;
@property (strong,
nonatomic) IBOutlet
UIImageView *img;
-(void)move:(id)sender;
@end
ViewController.m文件
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
@synthesize lab1;
@synthesize bt1;
@synthesize bt2;
@synthesize bt3;
@synthesize bt4;
@synthesize img;
- (void)viewDidLoad
{
lab1.text=@"走你";
//lab.backgroundColor=[UIColor blueColor];
[lab1
addSubview:img];
[bt1
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt1.tag=1;
[bt2
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt2.tag=2;
[bt3
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt3.tag=3;
[bt4
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt4.tag=4;
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)move:(id)sender{
UIButton *nwbt=sender;
switch (nwbt.tag) {
case 1:
lab1.center=CGPointMake(lab1.center.x,
lab1.center.y-30);
break;
case 2:
lab1.center=CGPointMake(lab1.center.x,
lab1.center.y+30);
break;
case 3:
lab1.center=CGPointMake(lab1.center.x-30,
lab1.center.y);
break;
case 4:
lab1.center=CGPointMake(lab1.center.x+30,
lab1.center.y);
break;
default:
break;
}
}
@end
xib文件的关联
实际效果图
ViewController.h文件
@interface ViewController :
UIViewController
{
UIButton * bt1;
UIButton * bt2;
UIButton *bt3;
UIButton *bt4;
UILabel * lab;
UIImageView *img;
}
@property (strong,
nonatomic) IBOutlet
UIButton * bt1;
@property (strong,
nonatomic) IBOutlet
UIButton * bt2;
@property (strong,
nonatomic) IBOutlet
UIButton * bt3;
@property (strong,
nonatomic) IBOutlet
UIButton * bt4;
@property (strong,
nonatomic) IBOutlet
UILabel * lab1;
@property (strong,
nonatomic) IBOutlet
UIImageView *img;
-(void)move:(id)sender;
@end
ViewController.m文件
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
@synthesize lab1;
@synthesize bt1;
@synthesize bt2;
@synthesize bt3;
@synthesize bt4;
@synthesize img;
- (void)viewDidLoad
{
lab1.text=@"走你";
//lab.backgroundColor=[UIColor blueColor];
[lab1
addSubview:img];
[bt1
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt1.tag=1;
[bt2
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt2.tag=2;
[bt3
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt3.tag=3;
[bt4
addTarget:self
action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
bt4.tag=4;
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)move:(id)sender{
UIButton *nwbt=sender;
switch (nwbt.tag) {
case 1:
lab1.center=CGPointMake(lab1.center.x,
lab1.center.y-30);
break;
case 2:
lab1.center=CGPointMake(lab1.center.x,
lab1.center.y+30);
break;
case 3:
lab1.center=CGPointMake(lab1.center.x-30,
lab1.center.y);
break;
case 4:
lab1.center=CGPointMake(lab1.center.x+30,
lab1.center.y);
break;
default:
break;
}
}
@end
xib文件的关联
实际效果图
相关文章推荐
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- iOS 百度地图路线绘制与小车平滑移动
- 动画:UIKitAnimation 简单动画学习 iOS (一) 渐变 、 移动 、翻页、来回翻转 ——转载
- ios实现简单随便移动的AR功能
- iOS简单动画效果:闪烁、移动、旋转、路径、组合、
- iOS文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写
- iOS简单动画效果:闪烁、移动、旋转、路径、组合
- qt-qml移动开发之在ios上开发和部署app流程简单介绍
- IOS 简单的动画自定义方法(旋转、移动、闪烁等)
- iOS文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写
- IOS 简单的动画自定义方法(旋转、移动、闪烁等)
- iOS 实现简单的移动UIView代码实例
- IOS 简单的动画自定义方法(旋转、移动、闪烁等)
- iOS table简单实现增加移动和删除功能与自定义cell的实现
- iOS 实现简单的移动UIView代码实例
- IOS-触摸简单应用-触摸移动按钮
- IOS 简单的动画自定义方法(旋转、移动、闪烁等)
- iOS 简单的动画自定义方法(旋转、移动、闪烁等)
- 动画:UIKitAnimation 简单动画学习 iOS (一) 渐变 、 移动 、翻页、来回翻转
- iOS- 简单说说iOS移动客户端SQLite3的基本使用