1.頁(yè)面搭建

image.png

利用代碼來(lái)搭建界面。

image.png

plist文件內(nèi)容

可見(jiàn)視圖所需要的屬性


*  左上的button
@property (nonatomic , strong) UIButton *leftUpButton;

*  右上的button
@property (nonatomic , strong) UIButton *rightUpButton;

*  左下的button
@property (nonatomic , strong) UIButton *leftDownButton;

*  右下的button
@property (nonatomic , strong) UIButton *rightDownButton;

*  右上方顯示金幣圖片及數(shù)量的button
@property (nonatomic , strong) UIButton *iconButton;

*  最上方顯示張數(shù) / 總數(shù)的label
@property (nonatomic , strong) UILabel *indexLabel;

*  標(biāo)題label
@property (nonatomic , strong) UILabel *titleLabel;

*  答案視圖
@property (nonatomic , strong) UIView *answerView;

*  選項(xiàng)視圖
@property (nonatomic , strong) UIView *optionView;

*  圖片button
@property (nonatomic , strong) UIButton *imageButton;

*  存放解析各種數(shù)據(jù)
@property (nonatomic , strong) NSMutableArray *dataArray;

*  按鈕計(jì)數(shù)器
@property (nonatomic , assign) NSInteger index;

*  幫助計(jì)數(shù)器
@property (nonatomic , assign) NSInteger helpIndex;

*  覆蓋類的視圖
@property (nonatomic , strong) CoverView *funcView;

*  滾動(dòng)類圖片
@property (nonatomic , strong) UIScrollView *changeView;

*  多圖頁(yè)數(shù)控制
@property (nonatomic , strong) UIPageControl *pageControl;

*  設(shè)置定時(shí)器
@property (nonatomic , strong) NSTimer *timer;

解析plist文件,創(chuàng)建模型

AppModel.h

@property(nonatomic,copy)NSString * answer;

@property(nonatomic,copy)NSString * icon;

@property(nonatomic,strong)NSArray * options;

@property(nonatomic,copy)NSString * title;
//外部調(diào)用接口
+(instancetype)appModelWithDic:(NSDictionary *)dic;
//存放答案
@property (nonatomic , strong) NSArray *answerArray;

AppModel.m

-(instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
        _answer = dic[@"answer"];
        _title = dic[@"title"];
        _options = dic[@"options"];
        _icon = dic[@"icon"];
        
        _answerArray = [self cutWithAnswer:dic[@"answer"]];
    }
    return self;
}


+(instancetype)appModelWithDic:(NSDictionary *)dic
{
    return [[self alloc] initWithDic:dic];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"icon === %@", _icon];
}
//剪切并放入數(shù)組
-(NSArray *)cutWithAnswer:(NSString *)answer{
    
    NSMutableArray *arr = [NSMutableArray array];
    
    for (int i = 0; i < answer.length; i++) {
        [arr addObject:[answer substringWithRange:NSMakeRange(i, 1)]];
    }
    
    return arr;
    
}

懶加載解析plist文件

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        NSArray * temp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"]];
        for (NSDictionary * dic in temp) {
            AppModel * model = [AppModel appModelWithDic:dic];
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}

來(lái)搭建視圖

- (void)viewDidLoad
{
    [super viewDidLoad];
    //背景色
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bj"]];
    //總數(shù)label
    _indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 25, 20, 50, 15)];
    
    _indexLabel.text = @"1/7";
    
    _indexLabel.textAlignment = NSTextAlignmentCenter;
    
    _indexLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_indexLabel];
    //金幣button
    _iconButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 95, 20, 90, 15)];
    
    [_iconButton setImage:[UIImage imageNamed:@"coin"] forState:UIControlStateNormal];
    
    [_iconButton setTitle:@"10000" forState:UIControlStateNormal];
    
    [self.view addSubview:_iconButton];
    //標(biāo)題
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, self.view.frame.size.width, 20)];
    
    _titleLabel.text = @"電影圖片";
    
    _titleLabel.lineBreakMode = 0;
    
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    
    _titleLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_titleLabel];
    //圖片button
    _imageButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, 150, 200, 200)];
    
    [_imageButton setBackgroundImage:[UIImage imageNamed:@"center_img"] forState:UIControlStateNormal];
    
    [_imageButton setImage:[UIImage imageNamed:@"movie_zghhr"] forState:UIControlStateNormal];
    
    UIEdgeInsets inset = {1,1,1,1};
    
    [_imageButton setImageEdgeInsets:inset];
    
    [self.view addSubview:_imageButton];
   
    
    //答案View
    _answerView = [[UIView alloc] initWithFrame:CGRectMake(0, _imageButton.frame.origin.y + _imageButton.frame.size.height + 25, self.view.frame.size.width, 40)];
    
    _answerView.backgroundColor = [UIColor clearColor];
   
    [self.view addSubview:_answerView];
     //選項(xiàng)視圖
    _optionView = [[UIView alloc] initWithFrame:CGRectMake(0, _answerView.frame.origin.y + _answerView.frame.size.height + 25, self.view.frame.size.width, self.view.frame.size.height - (_answerView.frame.origin.y + _answerView.frame.size.height + 25) - 25)];
    
    _optionView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:_optionView];
    /****************/
    [self addButton];
    [self loadData];

addButton(應(yīng)該封裝一個(gè)方法創(chuàng)建,暫時(shí)沒(méi)寫)

-(void)addButton
{
    
    _leftUpButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 150 + 5, 50,17)];
    
    [_leftUpButton setImage:[UIImage imageNamed:@"icon_tip"] forState:UIControlStateNormal];
    
    [_leftUpButton setTitle:@"提示" forState:UIControlStateNormal];
    
    _leftUpButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [_leftUpButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [_leftUpButton setBackgroundImage:[UIImage imageNamed:@"btn_left"] forState:UIControlStateNormal];
    
    [_leftUpButton setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted"] forState:UIControlStateHighlighted];
    
    [self.view addSubview:_leftUpButton];
    
    _rightUpButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 55, 150 + 5, 50, 17)];
    
    [_rightUpButton setImage:[UIImage imageNamed:@"icon_img"] forState:UIControlStateNormal];
    
    [_rightUpButton setTitle:@"多圖" forState:UIControlStateNormal];
    
    [_rightUpButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _rightUpButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [_rightUpButton setBackgroundImage:[UIImage imageNamed:@"btn_right"] forState:UIControlStateNormal];
    
    [_rightUpButton setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted"] forState:UIControlStateHighlighted];
    
    [self.view addSubview:_rightUpButton];
    
    _leftDownButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 150 + 200 - 17 - 5, 50, 17)];
    
    [_leftDownButton setImage:[UIImage imageNamed:@"icon_help"] forState:UIControlStateNormal];
    
    [_leftDownButton setTitle:@"幫助" forState:UIControlStateNormal];
    
    [_leftDownButton setTintColor:[UIColor whiteColor]];
    
    _leftDownButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [_leftDownButton setBackgroundImage:[UIImage imageNamed:@"btn_left"] forState:UIControlStateNormal];
    
    [_leftDownButton setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted"] forState:UIControlStateHighlighted];
    
    [self.view addSubview:_leftDownButton];
    
    _rightDownButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 55 , 150 + 200 - 17 - 5, 50, 17)];
    
    [_rightDownButton setTitle:@"下一題" forState:UIControlStateNormal];
    
    _rightDownButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    _rightDownButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [_rightDownButton setBackgroundImage:[UIImage imageNamed:@"btn_right"] forState:UIControlStateNormal];
    
    [_rightDownButton setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted"] forState:UIControlStateHighlighted];
    
    [self.view addSubview:_rightDownButton];
    
    [_leftUpButton addTarget:self action:@selector(buttonTip) forControlEvents:UIControlEventTouchUpInside];
    
    [_leftDownButton addTarget:self action:@selector(buttonHelp) forControlEvents:UIControlEventTouchUpInside];
    
    [_rightUpButton addTarget:self action:@selector(multipleImageButton) forControlEvents:UIControlEventTouchUpInside];
    
    [_rightDownButton addTarget:self action:@selector(buttonRightDown) forControlEvents:UIControlEventTouchUpInside];
}

loadData

-(void)loadData
{
    self.helpIndex = 0;

    AppModel *model = self.dataArray[_index];
    
     _indexLabel.text = [NSString stringWithFormat:@"%ld/%lu",_index + 1,self.dataArray.count];
    
    _titleLabel.text = model.title;
    _titleLabel.alpha = 0;
    
    [_imageButton setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
    
    CGFloat x = (_answerView.frame.size.width - 40 * model.answer.length - marginWidth * (model.answer.length - 1)) / 2;
    
    //答案按鈕
    for (int i = 0; i<model.answer.length; i++) {
        UIButton * tempButton = [[UIButton alloc] initWithFrame:CGRectMake(x + i*(marginWidth + 40) , 5, 40, 40)];
        [tempButton setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
        
        [tempButton setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
    
        tempButton.tag = AnswerButton + i;
        
        [_answerView addSubview:tempButton];
        
        [tempButton addTarget:self action:@selector(answerViewButton:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    //選擇的按鈕
    CGFloat y =0;
    NSInteger lineNumber = model.options.count/6 + 1;
    if (lineNumber > 1) {
        x = (_optionView.frame.size.width - 40 * 6 - marginWidth * (6 - 1)) / 2;
        y = (_optionView.frame.size.height - 40 * lineNumber - marginWidth*(lineNumber-1))/2;
    }else{
        x = (_optionView.frame.size.width - 40*model.options.count - marginWidth*(model.options.count - 1))/2;
        y = (_optionView.frame.size.height - 40)/2;
    }
    for (int i =0; i<lineNumber; i++) {
        for (int j =0; j<6; j++) {
            if ((i*6 +j +1)>model.options.count) {
                break;
            }
            UIButton * tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(x+j*(40+marginWidth), y + i*(40 + marginHeight), 40, 40)];
            
            [tmpButton setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:(UIControlStateNormal)];
            
            [tmpButton setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:(UIControlStateHighlighted)];
            
            [tmpButton setTitle:model.options[i*6 +j] forState:(UIControlStateNormal)];
            
            [tmpButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
            
            tmpButton.tag = OptionButton + i*6 +j;
            
            [_optionView addSubview:tmpButton];
            
            [tmpButton addTarget:self action:@selector(optionViewButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
        }
    }
}

基本的視圖搭建完成。下一章來(lái)搭建邏輯方面

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,383評(píng)論 4 61
  • SQL語(yǔ)言處理 本章,我們將會(huì)重點(diǎn)探討SQL語(yǔ)言基礎(chǔ),學(xué)習(xí)用SQL進(jìn)行數(shù)據(jù)庫(kù)的數(shù)據(jù)增加、刪除和修改等操作。另外請(qǐng)注...
    厲鉚兄閱讀 964評(píng)論 1 19
  • 晚上19:10分剛吃完晚飯,為了消食一個(gè)人在操場(chǎng)上走了一圈又一圈,也慢慢回想著這幾年發(fā)生的事,多少還是對(duì)感...
    楊小姐啊閱讀 250評(píng)論 0 0
  • 這是一級(jí)標(biāo)題 這是二級(jí)標(biāo)題 這是三級(jí)標(biāo)題 這是無(wú)序列表 看,無(wú)序列表。 這就是有序列表 有序 這個(gè)是引用,引用了一...
    Byron閱讀 224評(píng)論 0 1

友情鏈接更多精彩內(nèi)容