Crazy Guess chart Game

Source: Internet
Author: User
Tags event listener

#import "ViewController.h"

#import "WDJQuestions.h"

@interface Viewcontroller ()

/**

* Show Coins

*/

@property (Weak, nonatomic) Iboutlet UIButton *moneybtn;

/**

* Index

*/

@property (Weak, nonatomic) Iboutlet UILabel *lblindex;

/**

* Image Name

*/

@property (Weak, nonatomic) Iboutlet UILabel *lbldec;

/**

* Image Display button

*/

@property (Weak, nonatomic) Iboutlet UIButton *imagebtn;

/**

* Answer display area

*/

@property (Weak, nonatomic) Iboutlet UIView *answerview;

/**

* Option Display Area

*/

@property (Weak, nonatomic) Iboutlet UIView *optionview;

/**

* Next Question button

*/

@property (Weak, nonatomic) Iboutlet UIButton *nextbtn;

/**

* Model data

*/

@property (nonatomic, strong) Nsarray *questionsdata;

/**

* Index Value

*/

@property (nonatomic, assign) Nsinteger index;

/**

* Original Picture button frame

*/

@property (nonatomic, assign) CGRect oldimagebtnframe;

/**

* Shadow button

*/

@property (nonatomic, weak) UIButton *backgroundbtn;

/**

* Hint Mark

*/

@property (nonatomic, assign) BOOL prompt;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

_index = 0;

[Self showuiview];

}

#pragma mark-

#pragma mark-Next button

/**

* Next button click

*/

-(ibaction) Nextbtnclick {

In the next question, to open user interaction

_optionview.userinteractionenabled = YES;

_index++;

Prompt changes to No

_prompt = NO;

Add Next button click event

[Self showuiview];

}

#pragma mark-

#pragma mark-ui Display Interface

-(void) Showuiview {

Determine if subscript is out of bounds

if (_index >= self.questionsData.count) {

Return

}

Instantiating a model

Wdjquestions *quesdata = Self.questionsdata[_index];

Index display

_lblindex.text = [NSString stringwithformat:@ "%zd/%zd", _index+1,self.questionsdata.count];

Title Display

_lbldec.text = Quesdata.title;

Picture Display

[_imagebtn setimage:[uiimage ImageNamed:quesData.icon] forstate:uicontrolstatenormal];

Answer button Show

[Self answerview:quesdata];

The option Area button shows

[Self optionsview:quesdata];

If this is the last question, disable

self.nextBtn.enabled = ((_index+1)! = Self.questionsData.count);

}

#pragma mark-

#pragma mark-answer show area

-(void) Answerview: (wdjquestions *) Quesdata {

Before the call, delete the last

[Self.answerView.subviews makeobjectsperformselector: @selector (Removefromsuperview)];

Get Number of answer buttons

Nsinteger anscount = quesData.answer.length;

Button size

Nsinteger ANSBTNW = 40;

Nsinteger ANSBTNH = ANSBTNW;

button spacing

Nsinteger margin = 10;

Left spacing

CGFloat LeftMargin = (Self.answerview.bounds.size.width-anscount * ANSBTNW-(ansCount-1) * margin) * 0.5;

Loop Create button

for (Nsinteger i = 0; i < Anscount; i++) {

Button position

Ansbtnx

CGFloat ansbtnx = LeftMargin + (ansbtnw + margin) * I;

Ansbtny

CGFloat ansbtny = 0;

Instantiate button

UIButton *ansbtn = [[UIButton alloc] Initwithframe:cgrectmake (ansbtnx, Ansbtny, ANSBTNW, ANSBTNH)];

Button Background T-chart

[Ansbtn setbackgroundimage:[uiimage imagenamed:@ "Btn_answer"] forstate:uicontrolstatenormal];

[Ansbtn setbackgroundimage:[uiimage imagenamed:@ "btn_answer_highlighted"] forstate:uicontrolstatehighlighted];

Button text color

[Ansbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

Add Answer button click Method

[Ansbtn addtarget:self Action: @selector (Answerbtnclick:) forcontrolevents:uicontroleventtouchupinside];

Add to Ansview

[Self.answerview ADDSUBVIEW:ANSBTN];

}

}

#pragma mark-

#pragma mark-Answer Area click Button method

-(void) Answerbtnclick: (UIButton *) answerbtn {

After clicking, open the user interaction in the options area

_optionview.userinteractionenabled = YES;

Button text Delete

[Answerbtn Settitle:nil Forstate:uicontrolstatenormal];

After clicking the answer button the color turns black

[_answerview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

Type conversions

UIButton *ansbtn = obj;

Discoloration

[Ansbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

}];

Displays the button for the corresponding option area

[_optionview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

Type conversions

UIButton *optbtn = obj;

if (Optbtn.tag = = Answerbtn.tag) {

Optbtn.hidden = NO;

*stop = YES;

}

}];

Answerbtn.tag = 0;

}

#pragma mark-

#pragma mark-Option display area

-(void) Optionsview: (wdjquestions *) Quesdata {

Before the call is removed before the space

[Self.optionView.subviews makeobjectsperformselector: @selector (Removefromsuperview)];

Number of columns

Nsinteger cols = 7;

Number of options

Nsinteger quesdatacount = QuesData.options.count;

Button size

Nsinteger OPTIONBTNW = 40;

Nsinteger OPTIONBTNH = OPTIONBTNW;

button spacing

Nsinteger margin = 10;

Left spacing

CGFloat LeftMargin = (SELF.OPTIONVIEW.BOUNDS.SIZE.WIDTH-OPTIONBTNW * cols-(cols-1) * margin) * 0.5;

CGFloat topMargin = 0;

Loop Create button

for (Nsinteger i = 0; i < Quesdatacount; i++) {

Row and column index

Nsinteger colsnum = i% cols;

Nsinteger rowsnum = I/cols;

X-coordinate

CGFloat optionbtnx = LeftMargin + (optionbtnw + margin) * colsnum;

Y-coordinate

CGFloat Optionbtny = TopMargin + (OPTIONBTNH + margin) * rowsnum;

Set frame

UIButton *optionbtn = [[UIButton alloc] Initwithframe:cgrectmake (optionbtnx, Optionbtny, OPTIONBTNW, OPTIONBTNH)];

Background map

[Optionbtn setbackgroundimage:[uiimage imagenamed:@ "btn_option"] forstate:uicontrolstatenormal];

[Optionbtn setbackgroundimage:[uiimage imagenamed:@ "btn_option_highlighted"] forstate:uicontrolstatehighlighted];

Button Tag Value

Optionbtn.tag = i+1;

Button text color

[Optionbtn Settitle:quesdata.options[i] forstate:uicontrolstatenormal];

[Optionbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

Adding an option area event listener

[Optionbtn addtarget:self Action: @selector (Optionbtnclick:) forcontrolevents:uicontroleventtouchupinside];

Add to view

[Self.optionview ADDSUBVIEW:OPTIONBTN];

}

}

#pragma mark-

#pragma mark-Option Area Click Button method

-(void) Optionbtnclick: (UIButton *) optionbtn {

Button Hide

Optionbtn.hidden = YES;

Remove button text

NSString *title = Optionbtn.currenttitle;

Put the button text in the answer area

[_answerview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

Type conversions

UIButton *ansbtn = obj;

if (Ansbtn.currenttitle = = nil) {

[Ansbtn Settitle:title Forstate:uicontrolstatenormal];

Ansbtn.tag = Optionbtn.tag;

*stop = YES;

}

}];

Define variable markers to determine if the answer is complete

__block BOOL Finshans = YES;

Defines a mutable string, storing the answer area string

nsmutablestring *mstr = [nsmutablestring string];

Traverse the answer area

[_answerview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

UIButton *ansbtn = obj;

if (Ansbtn.currenttitle = = nil) {

Finshans = NO;

*stop = YES;

} else {

NSString *str = Ansbtn.currenttitle;

[Mstr Appendstring:str];

}

}];

Judge

if (Finshans = = YES) {

Turn off user interaction

_optionview.userinteractionenabled = NO;

Get the right answer

Wdjquestions *quesdata = Self.questionsdata[_index];

Comparison

if ([Quesdata.answer isequaltostring:mstr]) {

if (_index = = 9) {

The last question jumps to the first question

_index =-1;

}

Jump to the next question

[Self performselector: @selector (Nextbtnclick) Withobject:nil afterdelay:2.0];

Text is set to blue

[_answerview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

UIButton *ansbtn = obj;

[Ansbtn Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal];

}];

Gold +100

[Self newmoney:100];

if (!self.nextbtn.enabled) {

[Self helpbtnclick];

}

} else {

Text is set to red

[_answerview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

UIButton *ansbtn = obj;

[Ansbtn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];

}];

Gold-1000

[Self newmoney:-1000];

}

}

}

#pragma Mark calculates coins

-(void) Newmoney: (Nsinteger) var {

Get current coins

Nsinteger Currentmon = _moneybtn.currenttitle.intvalue;

Operating Gold

Nsinteger Nowmon = Currentmon + var;

Assign a value to a button

[_moneybtn settitle:[nsstring stringwithformat:@ "%zd", Nowmon] forstate:uicontrolstatenormal];

}

#pragma mark-

#pragma mark-click on the image to get bigger

-(ibaction) Bigbtnclick {

Original size acquisition

_oldimagebtnframe = Self.imageBtn.frame;

Set a new dimension

Nsinteger IMAGEBTNW = self.view.bounds.size.width;

Nsinteger IMAGEBTNH = IMAGEBTNW;

Set coordinates

Nsinteger imagebtnx = 0;

CGFloat Imagebtny = (SELF.VIEW.BOUNDS.SIZE.HEIGHT-IMAGEBTNH) * 0.5;

Create a background button picture

UIButton *backbtn = [[UIButton alloc] Initwithframe:cgrectmake (0, 0, 375, 667)];

Background map

[Backbtn setbackgroundimage:[uiimage imagenamed:@ "BJ"] forstate:uicontrolstatenormal];

SELF.BACKGROUNDBTN = backbtn;

Set transparency

Backbtn.alpha = 0;

Add to view

[Self.view AddSubview:self.backgroundBtn];

Put the picture on top

[Self.view BringSubviewToFront:self.imageBtn];

Set animation

[UIView animatewithduration:1.0 animations:^{

Self.imageBtn.frame = CGRectMake (Imagebtnx, Imagebtny, IMAGEBTNW, IMAGEBTNH);

Self.backgroundBtn.alpha = 0.7;

} completion:^ (BOOL finished) {

[Self.backgroundbtn addtarget:self Action: @selector (Backbtnclick) forcontrolevents:uicontroleventtouchupinside];

}];

}

#pragma mark Shadow click Method

-(void) Backbtnclick {

The picture gets smaller

[UIView animatewithduration:1.0 animations:^{

Self.imageBtn.frame = _oldimagebtnframe;

Self.backgroundBtn.alpha = 0;

} completion:^ (BOOL finished) {

[Self.backgroundbtn Removefromsuperview];

}];

}

#pragma mark-

#pragma mark-click on the image to get bigger

-(ibaction) Imagebtnclick {

Determine if there is a shadow

if (SELF.BACKGROUNDBTN) {

[Self backbtnclick];

}else {

[Self bigbtnclick];

}

}

#pragma mark-

#pragma mark-click the Prompt button event

-(ibaction) Promptbtnclick {

Tell if you've been prompted

if (_prompt) {

Return

}

_prompt = YES;

Gold reduction

[Self newmoney:-1000];

A button that simulates the user clicking on the answer area

[_answerview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

Type conversions

UIButton *ansbtn = obj;

Analog click

[Self answerbtnclick:ansbtn];

}];

Get answers

Wdjquestions *quesdata = Self.questionsdata[_index];

The first character to intercept the correct answer

NSString *fistans = [Quesdata.answer substringtoindex:1];

Determine the answer to this text and the options area

[_optionview.subviews enumerateobjectsusingblock:^ (__kindof UIView * _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {

Type conversions

UIButton *optbtn = obj;

Judge

if ([Optbtn.currenttitle Isequaltostring:fistans]) {

Analog Click Options button

[Self optionbtnclick:optbtn];

*stop = YES;

}

}];

}

#pragma mark-

#pragma mark-Help button click event

-(ibaction) Helpbtnclick {

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Congratulations on your clearance" message:@ "please call 110 to collect the bonus" Delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];

[Alertview show];

}

#pragma mark-

#pragma mark-Lazy loading method

-(Nsarray *) Questionsdata {

if (_questionsdata = = nil) {

Get path

NSString *path = [[NSBundle mainbundle] pathforresource:@ "questions.plist" oftype:nil];

Defining an array to store data

Nsarray *temparr = [Nsarray Arraywithcontentsoffile:path];

Defining a mutable array

Nsmutablearray *marr = [Nsmutablearray arrayWithCapacity:tempArr.count];

Dictionary Turn model

For (Nsdictionary *dict in Temparr) {

Instantiating a model

Wdjquestions *quedata = [Wdjquestions questionwithdict:dict];

Deposit into a mutable array

[Marr Addobject:quedata];

}

Assign value

_questionsdata = Marr;

}

return _questionsdata;

}

@end

Crazy Guess chart Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.