//
// ViewController.m
// 01-playPlane
//
// Created by 王 on 16/4/9.
// Copyright © 2016年 王. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUi];
// Do any additional setup after loading the view, typically from a nib.
}
//设置界面
- (void)setupUi{
//设置背景
UIImageView *backgroundPicture = [[UIImageView alloc]init];
//把背景设置好了
backgroundPicture.image = [UIImage imageNamed:@"background"];
//调整背景大小
backgroundPicture.frame = self.view.frame;
//添加到跟视图
[self.view addSubview:backgroundPicture];
//添加飞机
UIButton *plane = [[UIButton alloc]init];
//默认和高亮状态下的两张图片
UIImage *normal = [UIImage imageNamed:@"hero1"];
UIImage *highLight = [UIImage imageNamed:@"hero2"];
[plane setImage:normal forState:UIControlStateNormal];
[plane setImage:highLight forState:UIControlStateHighlighted];
//飞机按钮大小
[plane sizeToFit];
//飞机按钮位置
plane.center=self.view.center;
//添加到跟视图
[self.view addSubview:plane];
//添加方向按钮
CGFloat offset = 100;
[self addDirButtonWithImageName:@"top" offsetPoint:CGPointMake(0, -offset)];
[self addDirButtonWithImageName:@"left" offsetPoint:CGPointMake(-offset,0)];
[self addDirButtonWithImageName:@"right" offsetPoint:CGPointMake(offset, 0)];
[self addDirButtonWithImageName:@"bottom" offsetPoint:CGPointMake(0, offset)];
}
- (void)addDirButtonWithImageName:(NSString *)imageName offsetPoint:(CGPoint)offsetPoint{
//确定四个按钮中心点的位置
CGPoint center = CGPointMake(self.view.center.x, self.view.center.y+200);
//按钮的尺寸
CGFloat mySize = 40;
//确定按钮的frame
CGRect rect = CGRectMake(center.x-mySize*0.5, center.y-mySize*0.5, mySize, mySize);
UIButton *dirButton = [[UIButton alloc]init];
//
NSString *nName = [imageName stringByAppendingString:@"_normal"];
NSString *hName = [imageName stringByAppendingString:@"_highlighted"];
//
UIImage *normal = [UIImage imageNamed:nName];
UIImage *highLighted = [UIImage imageNamed:hName];
//
[dirButton setImage:normal forState:UIControlStateNormal];
[dirButton setImage:highLighted forState:UIControlStateHighlighted];
//位置
dirButton.frame = CGRectOffset(rect, offsetPoint.x, offsetPoint.y);
[self.view addSubview:dirButton];
}
@end
原文:http://www.cnblogs.com/bywjb/p/5376758.html