首页 > 代码库 > 应用程序开发之模仿史上最牛游戏(二)

应用程序开发之模仿史上最牛游戏(二)

  声明:转载请注明http://www.cnblogs.com/letougaozao/p/3708887.html

  • 新建关卡控制器
  • 自定义UIScrollView
  • 增加UIPageView
  • 每个关卡的View

整体效果展示

一、新建关卡控制器

1??拖线
-修改控制器class
-修改控制器的View的class(方便设置背景)
-装资源文件
-返回按钮

二、自定义UIScrollView
1??在初始化方法里面做一些事情
-添加四张背景图片
     1.创建四个FullView
     2.将这四个view加入到scrollview里面(封装成方法)
#pragma mark -加载图片
- (void)loadViews
{
    [self addView:@"select_easy_bg.jpg" index:0];
    [self addView:@"select_normal_bg.jpg" index:1];
    [self addView:@"select_hard_bg.jpg" index:2];
    [self addView:@"select_insane_bg.jpg" index:3];
}
#pragma mark加载图片
- (void)addView:(NSString*)viewName index:(int)index
{
    CGSize size = self.frame.size;
    BgView *bg = [[BgView alloc]initWithFrame:CGRectMake(size.width * index, 0, size.width, size.height)];
    [bg setFullView:viewName];
    [self addSubview:bg];
    [self sendSubviewToBack:bg];
}
-加载关卡信息
#pragma mark - 加载关卡
- (void)loadStages
{
    //1.从plist文件中加载所有的关卡
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"stages" withExtension:@"plist"];
    NSArray *stages = [NSArray arrayWithContentsOfURL:url];
    
    int count = stages.count;
    //2.将关卡中的字典取出,并且转换成一个模型
    for (int i = 0; i < count; i++) {
        NSDictionary *stageDic = stages[i];
        
        //2.1加载关卡的状态
        StageRecordModel *stageRecordModel =[[StageRecordTool sharedStageRecordTool] stageRecordWithNo:i + 1];
        
        //2.2加载关卡的信息
        StageInfo *stageInfo = [StageInfo stageInfoWith:stageDic];
        stageInfo.stageRecordModel = stageRecordModel;
        stageInfo.stageNo = i + 1;
        
        //2.3加载要显示的关卡的View
        Stage *stageView = [[NSBundle mainBundle]loadNibNamed:@"Stage" owner:nil options:nil][0];
        stageView.stageInfo = stageInfo;
        stageView.tag = i + 1;
        
        //2.4为stageView添加手势监听
        [stageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemClick:)]];
        
        //2.5计算每一个View所在的位置
        CGRect stageFrame = stageView.frame;
        CGFloat stageWidth = self.frame.size.width;
        CGFloat x = (stageWidth * 4 - stageFrame.size.width * 8)/8;
        stageFrame.origin.x = (stageFrame.size.width + x) * (i/3) + 27;
        stageFrame.origin.y = 120 + (i%3)*(stageFrame.size.width + 7);
        stageView.frame = stageFrame;
        
        [self addSubview:stageView];
        [self sendSubviewToBack:stageView];
    }
}
-设置scrollView的contentSize、可滚动、去掉滚动条
//3.设置scrollView的一些属性
        self.contentSize = CGSizeMake(self.frame.size.width * 4, self.frame.size.height);
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;

三、增加UIPageView
1??设置代理,监听是 scrollView的滚动
2??实现代理方法
3??设置pageView的current page
#pragma mark - 给pageView显示当前的页码
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageView.currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
}

四、关卡View(用xib描述)
1??新建Stage类
2??新建xib
3??新建一个Model和关卡信息一一对应
StageInfo.h
#import <Foundation/Foundation.h>
#import "StageRecordModel.h"

@interface StageInfo : NSObject

@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *intro;
@property (nonatomic, copy) NSString *format;
@property (nonatomic, copy) NSString *unit;

@property (nonatomic, assign) int stageNo;
@property (nonatomic, assign) double max;
@property (nonatomic, assign) double min;

//让关卡信息拥有一个StageRecordModel属性
@property (nonatomic, strong) StageRecordModel *stageRecordModel;

+ (StageInfo *)stageInfoWith:(NSDictionary*)dict;

@end
StageInfo.m
#import "StageInfo.h"
#define kIcon @"icon"
#define kTitle @"title"
#define kIntro @"intro"
#define kMax @"max"
#define kMin @"min"
#define kFormat @"format"
#define kUnit @"unit"

@implementation StageInfo

+ (StageInfo *)stageInfoWith:(NSDictionary *)dict
{
    StageInfo *stageInfo = [[self alloc]init];
    
    stageInfo.icon = dict[kIcon];
    stageInfo.title = dict[kTitle];
    stageInfo.intro = dict[kIntro];
    stageInfo.format = dict[kFormat];
    stageInfo.unit = dict[kUnit];
    stageInfo.max = [dict[kMax] doubleValue];
    stageInfo.min = [dict[kMin] doubleValue];
    
    return stageInfo;
}

@end
4??加载关卡信息
-Stage类
stage.h
#import <UIKit/UIKit.h>
@class StageInfo;

@interface Stage : UIView

@property (nonatomic, strong) StageInfo *stageInfo;
//关卡数
@property (weak, nonatomic) IBOutlet UIButton *stageNo;
//关卡图标
@property (weak, nonatomic) IBOutlet UIImageView *stageIcon;
//关卡锁
@property (weak, nonatomic) IBOutlet UIView *stageLock;
//关卡覆盖涂层
@property (weak, nonatomic) IBOutlet UIView *stageCover;
//关卡边框
@property (weak, nonatomic) IBOutlet UIImageView *stageRim;
//新关卡
@property (weak, nonatomic) IBOutlet UIImageView *stageNew;
//关卡等级下面的阴影
@property (weak, nonatomic) IBOutlet UIImageView *stageShadow;
//关卡的等级
@property (weak, nonatomic) IBOutlet UIImageView *stageRank;

@end
stage.m
@implementation Stage
+ (id)stageViewWithStageModel:(StageInfo *)stageInfo
{
    Stage *view = [[NSBundle mainBundle] loadNibNamed:@"StageView" owner:nil options:nil][0];
    view.stageInfo = stageInfo;
    return view;
}
#pragma mark 设置关卡信息
- (void)setStageInfo:(StageInfo *)stageInfo
{
    _stageInfo = stageInfo;
    // 1.设置编号
    [_stageNo setTitle:[NSString stringWithFormat:@"%d", stageInfo.no] forState:UIControlStateNormal];
    // 2.设置图标
    _stageImage.image = [UIImage imageNamed:stageInfo.icon];
}
@end
-添加关卡到StageView界面(查看二)