您的位置:首页 > 编程语言

纯代码实现小飞机游戏界面框架

2020-06-10 04:23 239 查看

//

//  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

 

转载于:https://www.cnblogs.com/bywjb/p/5376758.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐