• 疯狂猜图(点击图片放大)


     
     

    #import "ViewController.h"

    #import "JWQuestion.h"

    #import "NSArray+Log.h"

    #define kMargin 13

    @interfaceViewController ()

    // 1/10

    @property (weaknonatomicIBOutletUILabel *currenNumLabel;

    //  喜剧大片

    @property (weaknonatomicIBOutletUILabel *titleLabel;

    // 专门管理答案区域的Button

    @property (weaknonatomicIBOutletUIView *answerView;

    @property (weaknonatomicIBOutletUIView *optionView;

    @property (weaknonatomicIBOutletUIButton *iconBtn;

    // 灰灰的遮盖

    @property (strongnonatomicUIButton *cover;

    @property (strongnonatomicNSArray * questionDatas;

    @property(assignnonatomicint index;

    @property (weaknonatomicIBOutletUIButton *nextQuestionBtn;

    @end

    @implementation ViewController

    -(NSArray *)questionDatas

    {

        if (!_questionDatas) {

    //        NSArray *allDatas = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"]];

    //        

    //        NSMutableArray *tempArr = [NSMutableArray array];

    //        

    //        for (NSDictionary *dict in allDatas) {

    //         

    //            JWQuestion *question = [JWQuestion questionWithDict:dict];

    //            [tempArr addObject:question];

    //        }

    //        

            // JWQuestion对象

            _questionDatas = [JWQuestionquestion];

        }

        return_questionDatas;

    }

    -(UIButton *)cover

    {

        if (!_cover) {

            // 创建一个半透明的Button

            _cover = [[UIButtonalloc]initWithFrame:self.view.bounds];

            _cover.backgroundColor = [UIColorblackColor];

            _cover.alpha = 0.0;

            

            [_coveraddTarget:selfaction:@selector(bigImage:) forControlEvents:UIControlEventTouchUpInside];

            

            [self.viewaddSubview:_cover];

            self.cover = _cover;

        }

        return_cover;

    }

    - (void)viewDidLoad {

        [superviewDidLoad];

       

        self.index--;

        // 进来就加载数据

        [selfnextQuestion:nil];

    }

    // 点击大图

    - (IBAction)bigImage:(id)sender {

        //创建半透明的Button

        [selfcover];

        

        // 图片放到最上层

        

        [self.viewbringSubviewToFront:self.iconBtn];

        

        

        // 计算图片改变后的大小

        CGFloat iconBtnW = self.view.bounds.size.width;

        CGFloat iconBtnH = iconBtnW;

        CGFloat iconBtnY = (self.view.bounds.size.height - iconBtnW) * 0.5;

        

        // 判断现在是不是大图是大图就变小

        if (0.0 == self.cover.alpha) {   // 小图

            // 动画放大

            [UIViewanimateWithDuration:1.0 animations:^{

                

                self.cover.alpha = 0.5;

                

                self.iconBtn.frame = CGRectMake(0, iconBtnY, iconBtnW, iconBtnH);

            }];

        }

        else    // 已经是大图

        {

            [UIViewanimateWithDuration:1.0 animations:^{

                

                self.iconBtn.frame = CGRectMake(107 , 108, 160, 160);

                

                // alpha < 0.001的时候,不响应事件

                self.cover.alpha = 0.0;

            }];

        }

    }

    // 点击下一题

    - (IBAction)nextQuestion:(UIButton *)sender {

        

        self.index ++;

        

        // 取出数据

        JWQuestion *question = self.questionDatas[self.index];

        // 专门切换所有数据

        [selfchangeData:question];

        

        // 创建answer区域的按钮之前清空上次创建的按钮

        [selfcreatAnswerBtnWithQuestion:question];

        // 创建option区域的按钮

        [selfcreatOptionBtnWithQuestion:question];

    }

    // 专门切换所有数据

    -(void)changeData:(JWQuestion *)question

    {

        self.currenNumLabel.text = [NSStringstringWithFormat:@"%d/%ld",self.index + 1,self.questionDatas.count];

        self.titleLabel.text = question.title;

        

        [self.iconBtnsetImage:[UIImageimageNamed:question.iconforState:UIControlStateNormal];

        

        // 3> 不能点击

        self.nextQuestionBtn.enabled = self.index != self.questionDatas.count-1;

    }

    // 创建answer区域的按钮之前清空上次创建的按钮

    -(void)creatAnswerBtnWithQuestion:(JWQuestion *)question

    {

        for (UIButton *answerBtn inself.answerView.subviews) {

            [answerBtn removeFromSuperview];

        }

        // 拿出答案

        NSUInteger answerbtnCount = question.answer.length;

        

        CGFloat answerBtnW = 35;

        CGFloat answerBtnH = answerBtnW;

        

        // 不论几个按钮,都在屏幕中间;左边和右边间距一样

        

        CGFloat answerBtnStartX = (self.view.bounds.size.width - answerbtnCount * answerBtnW - (answerbtnCount - 1) * kMargin) * 0.5;

        

        for (int i = 0 ; i < answerbtnCount; i ++) {

            

            CGFloat answerBtnX = answerBtnStartX + (answerBtnW + kMargin) * i;

            

            UIButton *answerBtn = [[UIButtonalloc]initWithFrame:CGRectMake(answerBtnX, 0, answerBtnW, answerBtnH)];

            [answerBtn setBackgroundImage:[UIImageimageNamed:@"btn_answer"forState:UIControlStateNormal];

            [answerBtn setBackgroundImage:[UIImageimageNamed:@"btn_answer_highlighted"forState:UIControlStateHighlighted];

            

            [self.answerViewaddSubview:answerBtn];

        }

    }

    // 创建option区域的按钮

    -(void)creatOptionBtnWithQuestion:(JWQuestion *)question

    {

        // 个数== options数组的count   左边和右边间距一样

        

        for (UIButton *optionBtn inself.optionView.subviews) {

            [optionBtn removeFromSuperview];

        }

        

        NSUInteger optionBtnCount = question.options.count;

        

        CGFloat optionBtnW = 40;

        CGFloat optionBtnH = optionBtnW;

        

        int totolCol = 7;

        

        CGFloat optionBtnStartX = (self.view.bounds.size.width - totolCol *optionBtnW - (totolCol - 1) *kMargin) * 0.5;

        

        for (int i = 0 ; i < optionBtnCount; i ++) {

            

            int row = i / totolCol;

            int col = i % totolCol;

            

            CGFloat optionBtnX = optionBtnStartX + (optionBtnW + kMargin) *col;

            CGFloat optionBtnY = (optionBtnH + kMargin) * row;

            

            UIButton *optionBtn = [[UIButtonalloc]initWithFrame:CGRectMake(optionBtnX,optionBtnY , optionBtnW, optionBtnH)];

            

            [optionBtn setTitle:question.options[i] forState:UIControlStateNormal];

            [optionBtn setTitleColor:[UIColorblackColorforState:UIControlStateNormal];

            

            [optionBtn setBackgroundImage:[UIImageimageNamed:@"btn_option"forState:UIControlStateNormal];

            [optionBtn setBackgroundImage:[UIImageimageNamed:@"btn_option_highlighted"forState:UIControlStateHighlighted];

            

            [optionBtn addTarget:selfaction:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];

            

            [self.optionViewaddSubview:optionBtn];

        }

    }

    #warning 讲到这里了-----------

    -(void)optionBtnClick:(UIButton *)optionBtn

    {

        // 找到answer区域第一个没有titlebtn

        

        

        

        // option隐藏把title赋值给answerBtntitle

        

        optionBtn.hidden = YES;

        

    }

    @end

     
     
     
     
  • 相关阅读:
    C++学习(c++17)——2.X.使用string和string_view
    C++学习(c++17)——1.3一个小程序(part2.Datebase类)
    论++i + ++i + ++i
    C++学习(c++17)——1.3 一个小程序(part1.Employee类)
    C++学习(c++17)——1.2 稍微深入研究C++
    VS2019社区版关于Qt的设置
    C++学习(c++17)——1.1 C++基础知识
    2020杭电多校第五场
    2020杭电多校第四场
    2020杭电多校第三场
  • 原文地址:https://www.cnblogs.com/GhostKZShadow/p/5108773.html
Copyright © 2020-2023  润新知