标签:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//调用设置界面方法
[self setupUI];
}
//布置界面方法
#pragma mark - 布置界面
- (void)setupUI{
//放背景图片
//实例化一个视图
UIImageView *backgroundImageView=[[UIImageView alloc] init];
//设置视图大小
backgroundImageView.frame=self.view.frame;
//设置背景图片,放入视图中
backgroundImageView.image=[UIImage imageNamed:@"background"];
[self.view addSubview:backgroundImageView];
//放置飞机
//实例化一个按钮
UIButton *plane=[[UIButton alloc] init];
//定义两个图片
UIImage *planeNomal=[UIImage imageNamed:@"hero1"];
UIImage *planeHighlight=[UIImage imageNamed:@"hero2"];
//将两张图片设置到按钮上,对应两种状态
[plane setImage:planeNomal forState:UIControlStateNormal];
[plane setImage:planeHighlight forState:UIControlStateHighlighted];
//按照图片大小调整按钮大小
[plane sizeToFit];
//将飞机放到根视图中央
plane.center=self.view.center;
//将按钮添加到根视图
[self.view addSubview:plane];
//放按钮
//定义偏移量
CGFloat offset=40;
[self addDirButtonWithImageName:@"top" andOffsetPoint:CGPointMake(0, -offset)];
[self addDirButtonWithImageName:@"bottom" andOffsetPoint:CGPointMake(0, +offset)];
[self addDirButtonWithImageName:@"left" andOffsetPoint:CGPointMake(-offset, 0)];
[self addDirButtonWithImageName:@"right" andOffsetPoint:CGPointMake(+offset, 0)];
}
//方向按钮生成方法
#pragma mark - 生成方向按钮
- (void)addDirButtonWithImageName:(NSString *)imageName andOffsetPoint:(CGPoint)offsetPoint{
//设置按钮的长宽
CGFloat width=40;
CGFloat height=40;
//定义一个点表示四个按钮的中心点
CGPoint rect=CGPointMake(self.view.center.x, self.view.center.y+200);
CGRect center=CGRectMake(rect.x-width*0.5, rect.y-height*0.5, width, height);
//实例化一个按钮
UIButton *dirButton=[[UIButton alloc] init];
//设置按钮的大小和位置
dirButton.frame=CGRectOffset(center, offsetPoint.x, offsetPoint.y);
//根据参数生成图片名
NSString *nName=[imageName stringByAppendingString:@"_normal"];
NSString *hName=[imageName stringByAppendingString:@"_highlighted"];
//定义两个图片对象
UIImage *dirNormal=[UIImage imageNamed:nName];
UIImage *dirHighlighted=[UIImage imageNamed:hName];
//根据图片名将图片对象赋值给对应状态下的按钮
[dirButton setImage:dirNormal forState:UIControlStateNormal];
[dirButton setImage:dirHighlighted forState:UIControlStateHighlighted];
//将按钮添加到根视图
[self.view addSubview:dirButton];
}
@end
标签:
原文地址:http://www.cnblogs.com/baics/p/5375319.html