美文网首页
第一个实例项目

第一个实例项目

作者: 满大街都是大卡车 | 来源:发表于2016-04-25 13:25 被阅读0次

    从2015年12月29日开始一直在看OC的语法教程,希望在最短时间里先对语法进行学习,随后也跟着GitHub上的一个教程学做了一个app,一个打靶的小游戏。做完这个app以后想完全自己动手写一个app,然后就有了这个叫Draw其实就是一个抽奖的app。
    其实这个app功能挺简单,就是点击“开始抽奖”上方数字开始滚动,点击“停止”就会停止数字滚动,然后当前的数字就会被记录下来,当然也可以设置抽奖范围,因为默认的抽奖范围是1-500,基本功能就是这些,本来还想再设计一个一次抽奖人数,比如抽一次就出来五个数字这样的,可是能力有限暂时有点困难,等以后再完善吧。
    这个app是用storyboard做的,因为是单页面嘛比较简单一点。
    下面是代码:

    ViewController.h

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UILabel *rollLabel;//滚动显示区
    @property (weak, nonatomic) IBOutlet UILabel *showLabel;//中奖显示区
    @property (strong, nonatomic) IBOutlet UITextView *showNumber;//所有中奖显示区
    @property (weak, nonatomic) IBOutlet UITextField *rangeField;//随机范围输入框
    
    - (IBAction)beginDraw:(UIButton *)sender;//开始抽奖按钮
    - (IBAction)stopRoll:(UIButton *)sender;//停止抽奖按钮
    - (IBAction)cleanAll:(UIButton *)sender;//清空按钮
    - (IBAction)setButton:(UIButton *)sender;//设置按钮
    
    - (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 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 text = [self.rangeField.text integerValue];
            NSString *message = [NSString stringWithFormat:@"您已经成功将抽奖人数设置成%ld人", text];
            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 = @"请确认人数大于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 () <UINavigationBarDelegate>
    
    @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:^{}];
    }
    
    #pragma mark - UINavigationBarDelegate
    //扩大导航栏区域
    - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
        return UIBarPositionTopAttached;
    }
    
    @end
    

    总结一下通过这个简单的小工程学习到到一些地方:

    • 1.定时器的创建和实用
    myTimer = [NSTimer scheduledTimerWithTimeInterval:i
                                                 target:self
                                               selector:@selector(timerAction:)
                                               userInfo:nil
                                                repeats:YES];
    
    • 2.判断定时器是否停止,以及停止定时器
    if ([myTimer isValid]) {
        [myTimer invalidate];//关闭定时器
        myTimer = nil;//释放myTimer对象
    }
    
    • 3.警告提示框的使用
    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];
    
    • 4.创建自定义手势来隐藏键盘
    //创建手势
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
    
    //键盘隐藏的方法
    -(void)viewTapped:(UITapGestureRecognizer*)tapGr{
        [self.rangeField resignFirstResponder];
    }
    

    相关文章

      网友评论

          本文标题:第一个实例项目

          本文链接:https://www.haomeiwen.com/subject/uxtqrttx.html