本项目使用了 storyboard 和 xib
需求整理
-
一个滑动条
-
生成随机的目标数字
-
一个完成按钮
-
结果显示
-
积分累积 + 显示
-
轮数累积 + 显示
-
一个重新来过按钮
-
一个显示介绍的按钮
详细设计
-
横屏显示,禁止竖屏
-
页面布局:
-
拖动滑动条吧,让靶心尽可能接近目标;
-
滑块控件;
-
重新来过按钮控件、分数文本控件、轮数文本控件、介绍按钮控件;
-
-
( 0,100 ])
-
[1]得到成绩
-
弹出对话框,显示成绩。对话框中有“接着玩”和“不玩了”按钮。点击“接着玩”按钮,对话框消失,滑块恢复初始值,本次积分累积到分数,轮数加1;点击“不玩了”按钮,对话框消失,本次积分累积到到分数,轮数加1
-
点击显示介绍的按钮,弹出介绍页面
代码实现
-
横屏显示,禁止竖屏
-
Portrait 前的复选框取消选中
-
Landscape。这时,你会看到中间界面的虚拟手机屏幕已经由竖向变为横向了
-
-
设置界面
-
拖动滑动条吧,让靶心尽可能接近目标
-
上一个控件的下方、屏幕中心的位置
-
猜猜看
-
放置于屏幕底部,并为控件设置文本
-
-
(0, 100] 的随机整数
-
重新来过按钮设置点击事件
部分重点代码
-
ViewController.m
-
#import "ViewController.h"
-
#import "AboutViewController.h"
-
-
@interface ViewController ()
-
-
@property(nonatomic)int curVal; // 滑块当前的值
-
@property(nonatomic)int targetVal; // 目标值(随机生成的数)
-
@property(nonatomic)int score; // 得分
-
@property(nonatomic)int count; // 回合数
-
-
@property (weak, nonatomic) IBOutlet UISlider *slider;
-
// 分数显示文本
-
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
-
// 回合数显示文本
-
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
-
-
@end
-
-
@implementation ViewController
-
-
// 生成新的随机数,将滑块的值设置为50,并设置得分、回合数文本的显示值
- - (void)startNewRound {
- _targetVal = 1 + arc4random() % 100;
- _curVal = 50;
- _slider.value = _curVal;
- _scoreLabel.text = [NSString stringWithFormat:@"%d", _score];
- _countLabel.text = [NSString stringWithFormat:@"%d", _count];
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
// Do any additional setup after loading the view, typically from a nib.
- [self startNewRound];
- }
-
-
// 点击猜猜看按钮触发的事件
- - (IBAction)showAlert:(id)sender {
-
int diff = abs(_curVal - _targetVal);
-
int points = 100 - diff;
-
NSString *title = nil;
-
-
if (diff == 0) {
- title = @"土豪你太牛B了!";
- points += 100;
- } else if (diff < 5) {
- title = @"土豪你太棒了,差一点!";
-
if (diff == 1) {
- points += 50;
- }
- } else if (diff < 10) {
- title = @"好吧,勉强算个土豪!";
- } else {
- title = @"不是土豪少来装!";
- }
- _score += points;
- _count++;
-
-
NSString *msg = [NSString stringWithFormat:@"恭喜高富帅,你的得分是:%d", points];
-
UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
-
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"朕已知晓,爱卿辛苦了" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){
- [self startNewRound];
- }];
- [alertCtr addAction:alertAction];
- [self presentViewController:alertCtr animated:YES completion:nil];
-
- }
-
-
// 滑块值改变的事件
- - (IBAction)slideMove:(id)sender {
-
UISlider *slider = (UISlider *)sender;
- _curVal = (int)lroundf(slider.value);
- }
-
-
// 重新来过按钮点击事件
- - (IBAction)redo:(id)sender {
- _targetVal = 1 + arc4random() % 100;
- _curVal = 50;
- _score = 0;
- _count = 0;
- _slider.value = _curVal;
- _scoreLabel.text = [NSString stringWithFormat:@"%d", _score];
- _countLabel.text = [NSString stringWithFormat:@"%d", _count];
- }
-
-
// 显示信息事件
- - (IBAction)showInfo:(id)sender {
- AboutViewController *controller = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
- controller.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal;
- [self presentViewController:controller animated:YES completion:nil];
- }
-
@end
-
AboutViewController.m
-
#import "AboutViewController.h"
-
@interface AboutViewController ()
-
@end
-
@implementation AboutViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
// Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
-
// Dispose of any resources that can be recreated.
- }
- - (IBAction)close:(id)sender {
- [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
- }
-
@end
项目下载地址:点我下载
结束语
技术的道路,漫漫其修远兮。学习一门新的技术,总是伴随着各种并发症。但只要熬过前期,并坚持在中期,相信,后期一定是辉煌的。
-
使用滑块的值减去随机数,取结果的绝对值,绝对值加1就是最终结果。 ↩