第一个实例:Draw
来源:互联网 发布:mac快传 编辑:程序博客网 时间:2024/05/16 08:13
从2015年12月29日开始为一直在看OC的语法教程,希望在最短时间里先对语法进行学习,随后也跟着GitHub上的一个教程学做了一个app,一个打靶的小游戏。做完这个app以后为想完全自己动手写一个app,然后就有了这个叫Draw其实就是一个抽奖的app。
其实这个app功能挺简单,就是点击“开始抽奖”上方数字开始滚动,点击“停止”就会停止数字滚动,然后当前的数字就会被记录下来,当然也可以设置抽奖范围,因为默认的抽奖范围是1-500,基本功能就是这些,本来还想再设计一个一次抽奖人数,比如抽一次就出来五个数字这样的,可是能力有限暂时有点困难,等以后再完善吧。
这个app是用storyboard做的,因为是单页面嘛比较简单一点。
下面是代码:
ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController- (IBAction)beginDraw:(UIButton *)sender;//开始抽奖按钮- (IBAction)stopRoll:(UIButton *)sender;//停止抽奖按钮- (IBAction)cleanAll:(UIButton *)sender;//清空按钮- (IBAction)setButton:(UIButton *)sender;//设置按钮@property (weak, nonatomic) IBOutlet UILabel *rollLabel;//滚动显示区@property (weak, nonatomic) IBOutlet UILabel *showLabel;//中奖显示区@property (strong, nonatomic) IBOutlet UITextView *showNumber;//所有中奖显示区@property (weak, nonatomic) IBOutlet UITextField *rangeField;//抽奖人数输入框- (void)updateLabel;//更新Label文字- (void)rollshow;//滚动显示- (void)startAgain;//重新开始- (void)randomNumber;//生成随机数-(void)viewTapped:(UITapGestureRecognizer*)tapGr;//键盘隐藏- (void)timerAction:(NSTimer *)timer;//定时器@end
ViewController.m
#import "ViewController.h"@interface ViewController () { NSInteger randomValue; NSTimer *myTimer; NSInteger showValue; NSMutableString *allNumber; NSInteger randomRange;}@end@implementation ViewController//生成随机数方法- (void)randomNumber { if (randomRange == 0) { randomValue = (arc4random() % 500) + 1; }else { randomValue = (arc4random() % randomRange) + 1; } }//更新Label文字的方法- (void)updateLabel { self.rollLabel.text = [NSString stringWithFormat:@"%ld",randomValue]; self.showLabel.text = [NSString stringWithFormat:@"%ld",showValue]; self.showNumber.text = [NSString stringWithFormat:@"%@",allNumber];}//滚动显示方法- (void)rollshow { int i = 0.1; if (![myTimer isValid]) { myTimer = [NSTimer scheduledTimerWithTimeInterval:i target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; }}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. if (allNumber == nil) { allNumber = [NSMutableString stringWithFormat:@"本次中奖号码:"]; } [self updateLabel]; // 创建自定义的触摸手势来实现对键盘的隐藏 UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)]; tapGr.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapGr];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}//重新开始方法- (void)startAgain { randomValue = 0; showValue = 0; allNumber = [NSMutableString stringWithFormat:@"本次中奖号码:"]; self.rangeField.text = nil; [self updateLabel];}//开始抽奖按钮的方法- (IBAction)beginDraw:(UIButton *)sender { [self rollshow];}//停止抽奖按钮的方法- (IBAction)stopRoll:(UIButton *)sender { //先判断定时器是否在运行 if ([myTimer isValid]) { [myTimer invalidate];//关闭定时器 myTimer = nil;//释放myTimer对象 //拼接字符串,将产生的中奖号码进行拼接 allNumber = [NSMutableString stringWithFormat:@"%@ %ld",allNumber,randomValue]; } showValue = randomValue; [self updateLabel]; }//定时器的方法- (void)timerAction:(NSTimer *)timer { [self randomNumber]; [self updateLabel];}//清除按钮的方法- (IBAction)cleanAll:(UIButton *)sender { //提示框 NSString *title = @"清除数据"; NSString *message = @"注意!清除数据后中奖号码将无法恢复!"; NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil); NSString *otherButtonTitle = NSLocalizedString(@"确定", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self startAgain]; }]; [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil]; }//设置按钮的方法- (IBAction)setButton:(UIButton *)sender { //设置一个临时变量 NSInteger rangeNow = [self.rangeField.text integerValue]; if (rangeNow > 1) { randomRange = rangeNow; //提示框 NSString *title = @"设置成功"; NSInteger range = [self.rangeField.text integerValue]; NSString *message = [NSString stringWithFormat:@"您已经成功将抽奖人数设置为%ld人!",range]; NSString *otherButtonTitle = NSLocalizedString(@"确定", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { self.rangeField.text = nil; }]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil]; }else if (rangeNow <= 1) { //提示框 NSString *title = @"提示"; NSString *message = @"大哥,请确认人数以后再输入!\n抽奖人数要大于1人!!!"; NSString *otherButtonTitle = NSLocalizedString(@"确定", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { self.rangeField.text = nil; }]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil]; }}//键盘隐藏的方法-(void)viewTapped:(UITapGestureRecognizer*)tapGr{ [self.rangeField resignFirstResponder];}@end
第二个界面(纯粹为了学习做第二个界面才做的)
SetViewController.h
#import <UIKit/UIKit.h>@interface SetViewController : UIViewController- (IBAction)backButton:(UIButton *)sender;//返回按钮@end
SetViewController.m
#import "SetViewController.h"@interface SetViewController ()@end@implementation SetViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)backButton:(UIButton *)sender { [self dismissViewControllerAnimated:YES completion:^{}];}@end
主要学到的地方为:
定时器的创建和实用
myTimer = [NSTimer scheduledTimerWithTimeInterval:i target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
<pre name="code" class="objc">//定时器的方法- (void)timerAction:(NSTimer *)timer { [self randomNumber]; [self updateLabel];}判断定时器是否停止,以及停止定时器
if ([myTimer isValid]) { [myTimer invalidate];//关闭定时器 myTimer = nil;//释放myTimer对象 }
警告提示框的实用
//提示框 NSString *title = @"清除数据"; NSString *message = @"注意!清除数据后中奖号码将无法恢复!"; NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil); NSString *otherButtonTitle = NSLocalizedString(@"确定", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self startAgain]; }]; [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil];
创建自定义手势来隐藏键盘
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)]; tapGr.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapGr];
<pre name="code" class="objc">//键盘隐藏的方法-(void)viewTapped:(UITapGestureRecognizer*)tapGr{ [self.rangeField resignFirstResponder];}
0 0
- 第一个实例:Draw
- 第一个spring实例
- 第一个Ajax 实例
- 第一个jQuery实例
- 第一个struts2实例
- 第一个spring实例
- 第一个spring实例
- 第一个Lucene实例
- 第一个hibernate实例
- jquery 第一个实例
- 第一个spring实例
- 第一个WebService实例
- 第一个struts2实例
- SpringMVC第一个实例
- hibernate第一个实例
- 第一个jersey实例
- 第一个hibernate 实例
- 第一个Maven实例
- java static用法
- 【SpringBoot】ApplicationContextInitializer接口
- linux下内存的统计和内存泄露类问题的定位
- 【SSH进阶之路】Spring的AOP逐层深入——采用注解完成AOP(七)
- Windows Embedded Standard的U盘启动
- 第一个实例:Draw
- 基于Web浏览的用户行为分析
- 为什么我要用 Node.js
- [JAVA · 初级]:7.关键字:static
- 文章标题
- Message-oriented middleware
- 函数的值传递和引用传递
- 视图、索引、存储过程优缺点
- 【SSH进阶之路】一步步重构容器实现Spring框架——从一个简单的容器开始(八)