第一个实例:Draw

从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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值