这几天没有更新Blog,我一直在练习一个小项目,受益匪浅啊!
先吐槽一下,前两天遇到一个特别郁闷的事,可能是因为我免$99开发者费用导致的吧,经常性的一运行就崩溃……不过好在第三天的时候自己解决了,附当时的情况截图+解决方案:
遇到的情况(看见那绿绿的lldb了没。。。):
解决方案:将工程文件复制一份,删掉当前的工程文件,然后把复制的那份再复制一份,修改名称不要与之前的工程名称(仅文件夹名)不同,最后打开Xcode,重新加载这个工程文件(打开文件夹,选择 .xcodeproj 结尾的工程文件,打开再“Run”,OK,没有崩溃了……);顺便再说一下,对于初学者,不要给程序加断点,不然肯定运行就崩了,然后你也不知道错在哪……
写在前面:本项目是根据“让不懂编程的人爱上iPhone开发(2013秋iOS7版)”学习补充而来,主线思路是学习这个教程的,在此感谢该教程的作者王寒(还有他送的福利),下载地址:http://zhuanlan.zhihu.com/kidscoding/19584546
下面附上主界面:
附上源代码 头文件 .h :
//
// GJViewController.h
// 感觉测试
//
// Created by RunzeLee on 14-4-8.
// Copyright (c) 2014年 RunzeLee. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface GJViewController : UIViewController
//@synthesize 和 @property(在.h文件中的 @interface和@end之间) 的用法参考:http://justcoding.iteye.com/blog/1444548
@property (strong,nonatomic) IBOutlet UISlider *slider;
@end
附上源代码 实现文件 .m :
//
// GJViewController.m
// 感觉测试
//
// Created by RunzeLee on 14-4-8.
// Copyright (c) 2014年 RunzeLee. All rights reserved.
//
#import "GJViewController.h"
@interface GJViewController (){
//_currentValue = 产生随机数 0 - 100
//_targetValue = 触动的数值 0 - 100
//_score = 总分
//_round = 次数
int _currentValue;
int _targetValue;
int _score;
int _round;
}
- (IBAction)About:(id)sender; //右下角“关于”Button的连接
- (IBAction)sliderMoved:(UISlider*)sender; //中间的“拉条”Slider的连接
- (IBAction)start:(id)sender; //“提交数据”Button的连接
@property (strong, nonatomic) IBOutlet UILabel *targetLabel; //“目标数值”后面的那个Label(显示为红色的0)的连接
@property (strong, nonatomic) IBOutlet UILabel *scoreLabel; //“累计分数”后面的那个Label(显示为黑色的0)的连接
@property (strong, nonatomic) IBOutlet UILabel *roundLabel; //“完成次数”后面的那个Label(显示为黑色的0)的连接
@end
@implementation GJViewController
//@synthesize 和 @property(在.h文件中的 @interface和@end之间) 的用法参考:http://justcoding.iteye.com/blog/1444548
@synthesize slider;
@synthesize targetLabel;
@synthesize scoreLabel;
//左下角“重现来过”Button的直接功能:
- (IBAction)clearAll:(UIButton *)sender {
_score = 0;
_round = 0;
self.scoreLabel.text = [NSString stringWithFormat:@"%d",_score];
self.roundLabel.text = [NSString stringWithFormat:@"%d",_round];
[self startNewRound];
self.targetLabel.text = [NSString stringWithFormat:@"%d",_targetValue];
NSLog(@"数据已经重置!");
[[[UIAlertView alloc]initWithTitle:@"新的一局" message:@"你已开始新的测试……" delegate:nil cancelButtonTitle:@"要的" otherButtonTitles:nil, nil]show];
}
//数据更新(实现方法):
- (void)updateLabels{
self.targetLabel.text = [NSString stringWithFormat:@"%d",_targetValue];
self.scoreLabel.text = [NSString stringWithFormat:@"%d",_score];
self.roundLabel.text = [NSString stringWithFormat:@"%d",_round];
}
//新的一次(实现方法):
- (void)startNewRound{
_targetValue = arc4random()%100;
_currentValue = 0;
self.slider.value = _currentValue;
}
//入口(初始化):
- (void)viewDidLoad
{
[super viewDidLoad];
[self startNewRound];
[self updateLabels];
// Do any additional setup after loading the view, typically from a nib.
}
//内存警告处理的块,暂时没用到。。
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//右下角“关于”的实现方法:
- (IBAction)About:(id)sender {
[[[UIAlertView alloc]initWithTitle:@"欢迎" message:@"这是一个开发测试版本,我们会告诉你一个数值,你将凭借你的感觉滑动滑条,让它对应的数值最接近目标数值,祝你好运! —— Ziwer Inc" delegate:nil cancelButtonTitle:@"要的" otherButtonTitles:nil, nil]show];
}
//当用户滚动中间的“拉条”时触发的方法:
- (IBAction)sliderMoved:(UISlider*)sender {
_currentValue = (int)lroundf(self.slider.value);
NSLog(@"拉条的值:%d",_currentValue);
}
//当用户Click“提交数据”Button时触发的方法:
- (IBAction)start:(id)sender {
//定义差值变量和得分变量,difference是两个数据的差,points是所得分数。
int points = 0;
int difference = abs(_currentValue - _targetValue);
points = 100 - difference;
NSString *title,*title2=@""; //初始化当用户获得⭐️的时候补充的一句赞美的话为无内容,如果没有 =@“” 这一小段的话,假如后面没有给title2附值,输出将会是(null),影响美观。
if (difference == 0) {
title = @"⭐️⭐️⭐️";
title2 = @"完美触觉!奖励50分!";
points += 50;
} else if (difference <= 5) {
title = @"⭐️⭐️ ";
title2 = @"很完美了!";
if (difference == 1) {
title2 = @"仅差一点!奖励20分!";
points += 20;
}
} else if (difference <=20) {
title = @"⭐️ ";
title2 = @"继续加油!";
} else if (difference <= 40) {
title = @"⚠你看错了吧";
} else if (difference <= 99) {
title = @"⚠这是嘛技术啊……";
} else {
title = @"⚠你是故意的吧";
}
_score += points; //将获得的分数(包括100-差值获得的分数和差值=0获得的50或差值=1获得的20分)加入总和
_round += 1; //完成次数 + 1
NSLog(@"已完成次数:%d",_round);
//反馈消息统计:
NSString *message = [NSString stringWithFormat:@"%@\n滑动条的当前数值是: %d\n目标的值是:%d\n两者的差距是:%d\n您获得了[%d]分!",title2,_currentValue,_targetValue,difference,points];
//反馈消息输出:
[[[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:@"要的" otherButtonTitles:nil, nil]show];
//新的一次(初始化):
[self startNewRound];
[self updateLabels];
}
@end
我已经对代码们都做了详细的注释,刚兴趣的朋友可以读一读,有好的想法可以相互交流!
工程文件加密共享,不公开,欢迎找我索取。
ipa文件下载地址(越狱的朋友们可以体验一下): http://yunpan.cn/QNyLMUiAECJeV
下面附上真机运行图: