IOS UI开发基础之超级猜图完整版本-08
#import "ViewController.h"
#import "CZQuestion.h"
@interface ViewController ( )
- ( IBAction) btnIconClick: ( id) sender;
@property ( nonatomic, assign) CGRect iconFrame;
@property ( nonatomic, strong) NSArray * questions;
@property ( nonatomic, assign) int index;
@property ( weak, nonatomic) IBOutlet UILabel * lblIndex;
@property ( weak, nonatomic) IBOutlet UIButton * btnScore;
@property ( weak, nonatomic) IBOutlet UILabel * lblTitle;
@property ( weak, nonatomic) IBOutlet UIButton * btnIcon;
@property ( weak, nonatomic) IBOutlet UIButton * btnNext;
@property ( weak, nonatomic) UIButton * cover;
@property ( weak, nonatomic) IBOutlet UIView * answerView;
@property ( weak, nonatomic) IBOutlet UIView * optionsView;
- ( IBAction) btnTipClick;
- ( IBAction) btnNextClick;
- ( IBAction) bigImage: ( id) sender;
@end
@implementation ViewController
- ( NSArray * ) questions{
if ( _questions== nil) {
NSString * path = [ [ NSBundle mainBundle] pathForResource: @"questions.plist" ofType: nil] ;
NSArray * arrayDict = [ NSArray arrayWithContentsOfFile: path] ;
NSMutableArray * arrMutable = [ NSMutableArray array] ;
for ( NSDictionary * dict in arrayDict) {
CZQuestion * model = [ CZQuestion questionWithDict: dict] ;
[ arrMutable addObject: model] ;
}
_questions = arrMutable;
}
return _questions;
}
- ( UIStatusBarStyle) preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- ( BOOL) prefersStatusBarHidden{
return YES;
}
- ( void ) viewDidLoad {
[ super viewDidLoad] ;
self . index = - 1 ;
[ self nextQuestion] ;
}
- ( IBAction) bigImage: ( id) sender {
self . iconFrame = self . btnIcon. frame;
UIButton * btnCover = [ [ UIButton alloc] init] ;
btnCover. frame = self . view. bounds;
btnCover. backgroundColor = [ UIColor blackColor] ;
btnCover. alpha = 0.0 ;
[ self . view addSubview: btnCover] ;
[ btnCover addTarget: self action: @selector ( smallImage) forControlEvents: UIControlEventTouchUpInside] ;
[ self . view bringSubviewToFront: self . btnIcon] ;
self . cover = btnCover;
CGFloat iconW = self . view. frame. size
. width;
CGFloat iconH = iconW;
CGFloat iconX = 0 ;
CGFloat iconY = ( self . view. frame. size. height - iconH) * 0.5 ;
[ UIView animateWithDuration: 0.7 animations: ^ {
btnCover. alpha = 0.6 ;
self . btnIcon. frame = CGRectMake ( iconX, iconY, iconW, iconH) ;
} ] ;
}
- ( IBAction) btnNextClick {
[ self nextQuestion] ;
}
- ( IBAction) btnTipClick {
[ self addScore: - 1000 ] ;
for ( UIButton * btnAnswer in self . answerView. subviews) {
[ self btnAnswerClick: btnAnswer] ;
}
CZQuestion * model = self . questions[ self . index] ;
NSString * firstChar = [ model. answer substringToIndex: 1 ] ;
for ( UIButton * btnOpt in self . optionsView. subviews) {
if ( [ btnOpt. currentTitle isEqualToString: firstChar] ) {
[ self optionButtonClick: btnOpt] ;
break ;
}
}
}
- ( void ) nextQuestion{
self . index++ ;
if ( self . index == self . questions. count) {
NSLog ( @"答题完毕" ) ;
return ;
}
CZQuestion * model= self . questions[ self . index] ;
[ self settingData: model] ;
[ self makeAnswerButton: model] ;
[ self makeOptionsButton: model] ;
}
- ( void ) makeOptionsButton: ( CZQuestion * ) model{
self . optionsView. userInteractionEnabled = YES;
[ self . optionsView. subviews makeObjectsPerformSelector: @selector ( removeFromSuperview) ] ;
NSArray * words = model. options;
CGFloat optionW = 35 , optionH= 35 ;
CGFloat margin = 10 ;
int colums = 7 ;
CGFloat marginLeft = ( self . optionsView. frame. size. width - colums* optionW - ( colums- 1 ) * margin) * 0.5 ;
for ( int i= 0 ; i< words. count; i++ ) {
UIButton * btnOpt = [ [ UIButton alloc] init] ;
btnOpt. tag = i;
[ btnOpt setBackgroundImage: [ UIImage imageNamed: @"btn_option" ] forState: UIControlStateNormal] ;
[ btnOpt setBackgroundImage: [ UIImage imageNamed: @"btn_option_highlighted" ] forState: UIControlStateHighlighted] ;
[ btnOpt setTitle: words[ i] forState: UIControlStateNormal] ;
[ btnOpt setTitleColor: [ UIColor blueColor] forState: UIControlStateNormal] ;
int colIdx = i% colums;
int rowIdx = i/ colums;
CGFloat optionX = marginLeft + colIdx* ( optionW+ margin) ;
CGFloat optionY = 0 + rowIdx* ( optionH+ margin) ;
btnOpt. frame = CGRectMake ( optionX, optionY, optionW, optionH) ;
[ self . optionsView addSubview: btnOpt] ;
[ btnOpt addTarget: self action: @selector ( optionButtonClick: ) forControlEvents: UIControlEventTouchUpInside] ;
}
}
- ( void ) optionButtonClick: ( UIButton * ) sender{
sender. hidden = YES;
NSString * text = sender. currentTitle;
for ( UIButton * answerBtn in self . answerView. subviews) {
if ( answerBtn. currentTitle== nil) {
[ answerBtn setTitle: text forState: UIControlStateNormal] ;
answerBtn. tag = sender. tag;
break ;
}
}
BOOL isFull = YES;
NSMutableString * userInput = [ NSMutableString string] ;
for ( UIButton * btnAnswer in self . answerView. subviews) {
if ( btnAnswer. currentTitle== nil) {
isFull = NO;
break ;
} else {
[ userInput appendString: btnAnswer. currentTitle] ;
}
}
if ( isFull) {
self . optionsView. userInteractionEnabled = NO;
CZQuestion * model= self . questions[ self . index] ;
if ( [ model. answer isEqualToString: userInput] ) {
[ self addScore: 100 ] ;
[ self setAnswerButtonColor: [ UIColor blueColor] ] ;
[ self performSelector: @selector ( nextQuestion) withObject: nil afterDelay: 0.5 ] ;
} else {
[ self setAnswerButtonColor: [ UIColor redColor] ] ;
}
}
}
- ( void ) addScore: ( int ) score{
NSString * str= self . btnScore. currentTitle;
int currentScore = str. intValue;
currentScore = currentScore + score;
[ self . btnScore setTitle: [ NSString stringWithFormat: @"%d" , currentScore] forState: UIControlStateNormal] ;
}
- ( void ) setAnswerButtonColor: ( UIColor * ) color{
for ( UIButton * btnAnswer in self . answerView. subviews) {
[ btnAnswer setTitleColor: color forState: UIControlStateNormal] ;
}
}
- ( void ) smallImage{
[ UIView animateWithDuration: 0.7 animations: ^ {
self . btnIcon. frame = self . iconFrame;
self . cover. alpha = 0.0 ;
} completion: ^ ( BOOL finished) {
[ self . cover removeFromSuperview] ;
self . cover = nil;
} ] ;
}
- ( IBAction) btnIconClick: ( id) sender {
if ( self . cover== nil) {
[ self bigImage: nil] ;
} else {
[ self smallImage] ;
}
}
- ( void ) settingData: ( CZQuestion * ) model{
self . lblIndex. text= [ NSString stringWithFormat: @"%d / %ld" , ( self . index + 1 ) , self . questions. count] ;
self . lblTitle. text= model. title;
[ self . btnIcon setImage: [ UIImage imageNamed: model. icon] forState: UIControlStateNormal] ;
self . btnNext. enabled = ( self . index != self . questions. count - 1 ) ;
}
- ( void ) makeAnswerButton: ( CZQuestion * ) model{
[ self . answerView. subviews makeObjectsPerformSelector: @selector ( removeFromSuperview) ] ;
NSUInteger len = model. answer. length;
CGFloat margin = 10 ;
CGFloat answerW = 35 ;
CGFloat answerH = answerW;
CGFloat answerY = 0 ;
CGFloat marginLeft = ( self . answerView. frame. size. width- ( len* answerW) - ( len- 1 ) * margin) / 2 ;
for ( int i= 0 ; i< len; i++ ) {
UIButton * btnAnswer = [ [ UIButton alloc] init] ;
[ btnAnswer setBackgroundImage: [ UIImage imageNamed: @"btn_answer" ] forState: UIControlStateNormal] ;
[ btnAnswer setBackgroundImage: [ UIImage imageNamed: @"btn_answer_highlighted" ] forState: UIControlStateHighlighted] ;
CGFloat answerX = marginLeft + i * ( answerW + margin) ;
btnAnswer. frame = CGRectMake ( answerX, answerY, answerW, answerH) ;
[ btnAnswer setTitleColor: [ UIColor blackColor] forState: UIControlStateNormal] ;
[ self . answerView addSubview: btnAnswer] ;
[ btnAnswer addTarget: self action: @selector ( btnAnswerClick: ) forControlEvents: UIControlEventTouchUpInside] ;
}
}
- ( void ) btnAnswerClick: ( UIButton * ) sender {
self . optionsView. userInteractionEnabled = YES;
[ self setAnswerButtonColor: [ UIColor blackColor] ] ;
for ( UIButton * optBn in self . optionsView. subviews) {
if ( sender. tag== optBn. tag) {
optBn. hidden = NO;
break ;
}
}
[ sender setTitle: nil forState: UIControlStateNormal] ;
}
@end
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface CZQuestion : NSObject
@property ( nonatomic, copy) NSString * answer;
@property ( nonatomic, copy) NSString * icon;
@property ( nonatomic, copy) NSString * title;
@property ( nonatomic, strong) NSArray * options;
- ( instancetype) initWithDict: ( NSDictionary * ) dict;
+ ( instancetype) questionWithDict: ( NSDictionary * ) dict;
@end
NS_ASSUME_NONNULL_END
#import "CZQuestion.h"
@implementation CZQuestion
- ( instancetype) initWithDict: ( NSDictionary * ) dict{
if ( self == [ super init] ) {
self . answer = dict[ @"answer" ] ;
self . title= dict[ @"title" ] ;
self . icon= dict[ @"icon" ] ;
self . options= dict[ @"options" ] ;
}
return self ;
}
+ ( instancetype) questionWithDict: ( NSDictionary * ) dict{
return [ [ self alloc] initWithDict: dict] ;
}
@end