码迷,mamicode.com
首页 > 其他好文 > 详细

长按按钮实现数值快速增长和快速减小

时间:2015-05-04 12:01:01      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:uibutton   label   uilabel   游戏   应用   

不管在应用中还是游戏中让用户输入数字都是比较麻烦的,首先需要呼出键盘,其次输入数字,最后确认输入,在这么小的屏幕上步骤如此麻烦,假想,如果让用户长按按钮实现数值快速增长和快速减小,那多简单啊,好吧,废话不多说,直接上代码,实现比较简单,不做过多解释(如有不当之处,烦请指出,多谢)

技术分享

//
//  ViewController.m
//
//  Created by Hunk on 15/2/12.
//  Copyright (c) 2015年 Hunk. All rights reserved.
//

#import "ViewController.h"

#define STAGE_ONE       (20)
#define STAGE_TWO       (50)
#define STAGE_THREE     (100)

#define MINUS_BTN_TAG   (99)
#define PLUS_BTN_TAG    (101)

@interface ViewController ()
{
    UILabel *label;
    
    NSTimer *timer;
    
    NSInteger flag;
    
    NSInteger index;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    index = 1;
    flag  = 0;
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 100.0, CGRectGetWidth(self.view.frame) - 100.0, 60.0)];
    [label setBackgroundColor:[UIColor yellowColor]];
    [label setFont:[UIFont systemFontOfSize:50.0]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setText:@(index).stringValue];
    [self.view addSubview:label];
    
    UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [minusButton setFrame:CGRectMake(0.0, CGRectGetMinY(label.frame), 50.0, CGRectGetHeight(label.frame))];
    [minusButton setBackgroundColor:[UIColor redColor]];
    [minusButton setTitle:@"-" forState:UIControlStateNormal];
    [minusButton.titleLabel setFont:[UIFont systemFontOfSize:50]];
    [minusButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [minusButton setTag:MINUS_BTN_TAG];
    [self.view addSubview:minusButton];
    
    UILongPressGestureRecognizer *minusLongPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
    [minusLongPressGestureRecognizer addTarget:self action:@selector(minusGestureRecognizerAction:)];
    [minusLongPressGestureRecognizer setMinimumPressDuration:.5f];
    [minusLongPressGestureRecognizer setAllowableMovement:10.0];
    [minusButton addGestureRecognizer:minusLongPressGestureRecognizer];
    

    UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [plusButton setFrame:CGRectMake(CGRectGetWidth(self.view.frame) - 50.0, CGRectGetMinY(label.frame), 50.0, CGRectGetHeight(label.frame))];
    [plusButton setBackgroundColor:[UIColor greenColor]];
    [plusButton setTitle:@"+" forState:UIControlStateNormal];
    [plusButton.titleLabel setFont:[UIFont systemFontOfSize:50]];
    [plusButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [plusButton setTag:PLUS_BTN_TAG];
    [self.view addSubview:plusButton];
    
    UILongPressGestureRecognizer *plusLongPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
    [plusLongPressGestureRecognizer addTarget:self action:@selector(plusGestureRecognizerAction:)];
    [plusLongPressGestureRecognizer setMinimumPressDuration:.5f];
    [plusLongPressGestureRecognizer setAllowableMovement:10.0];
    [plusButton addGestureRecognizer:plusLongPressGestureRecognizer];
}

- (void)buttonAction:(UIButton *)button
{
    if(MINUS_BTN_TAG == [button tag])
    {
        index--;
        
        if(index < 1)
        {
            index = 1;
        }
    }
    else if(PLUS_BTN_TAG == [button tag])
    {
        index++;
    }
    
    [label setText:@(index).stringValue];
}

- (void)minusGestureRecognizerAction:(UILongPressGestureRecognizer *)panGestureRecognizer
{
    switch(panGestureRecognizer.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            flag = -1;
            
            timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [timer invalidate];
            timer = nil;
        }
            break;
        default:
            break;
    }
}

- (void)plusGestureRecognizerAction:(UILongPressGestureRecognizer *)panGestureRecognizer
{
    switch(panGestureRecognizer.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            flag = 1;
            
            timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
            
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [timer invalidate];
            timer = nil;
        }
            break;
        default:
            break;
    }
}

- (void)updateTimer:(NSTimeInterval)dt
{
    if(index > 0 && index < STAGE_ONE)
    {
        index += (1 * flag);
    }
    else if(index >= STAGE_ONE && index < STAGE_TWO)
    {
        index += (5 * flag);
    }
    else if(index >= STAGE_TWO && index < STAGE_THREE)
    {
        index += (10 * flag);
    }
    else
    {
        index += (50 * flag);
    }
    
    if(index < 1)
    {
        index = 1;
    }
    
    [label setText:@(index).stringValue];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


长按按钮实现数值快速增长和快速减小

标签:uibutton   label   uilabel   游戏   应用   

原文地址:http://blog.csdn.net/lixuwen521/article/details/45477685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!