2

这是我的主屏幕

(IOS 开发新手)这是我的主屏幕,这个屏幕是 .ccbi 文件,按钮是我在代码中链接并实现方法的图像。我正在做的是当我单击加号按钮时,在负号按钮的情况下,值会增加一个。但每次我都必须单击按钮来增加/减少值。我希望当我按下按钮时,值应该不断增加,直到我取消触摸按钮。?我怎样才能做到这一点?我在某处读到它可以使用 CCLayer TouchBegin(); 但我找不到帮助我理解机制的材料。有什么建议么?

4

4 回答 4

3

您可以简单地使用一个计时器和两个 IBAction 来完成。

声明一个计时器和两个 IBAction,例如:

@property (nonatomic) NSTimer *timer;
- (IBAction)inc:(id)sender;
- (IBAction)stop:(id)sender;

并实现如下方法:

- (IBAction)increment:(id)sender
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(inc) userInfo:nil repeats:YES];
}
- (IBAction)stop:(id)sender
{
    [_timer invalidate];
}

- (void)inc
{
    //Increment and set the text value here
}

最重要的事情:

increment:将函数连接到touch down您的事件UIButton并将stop:函数连接到touch up inside事件

于 2013-07-18T06:36:07.607 回答
1

您可以只使用一种由“touch down”触发的方法,而不是通常的“touch up inside”。该方法将启动一个计时器,该计时器将以您想要的任何速率更新标签。您将有另一种方法进行修饰来阻止这种情况。

编辑后:

如果您有图像而不是按钮,则必须使用手势识别器来执行此操作。我创建了一个 UITapGestureRecognizer 的子类并覆盖了 touchesBegan 和 touchesEnded,它们每个都调用一个由视图控制器类处理的委托方法。

RDTapper.h

@protocol TapperTapped <NSObject>

-(void)touchedDownOnTapper;
-(void)touchedUpOnTapper;

@end

@interface RDTapper : UITapGestureRecognizer

@property (weak,nonatomic) id <TapperTapped> delegate;

RDTapper.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.delegate touchedDownOnTapper];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.delegate touchedUpOnTapper];
}

视图控制器.m

@interface ViewController ()
@property (weak,nonatomic) IBOutlet UILabel *label;
@property (strong,nonatomic) NSTimer *aTimer;
@property (strong,nonatomic) NSDate *startTime;
@property (weak,nonatomic) IBOutlet RDTapper *tapper;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tapper.delegate = self;
}

-(void)touchedDownOnTapper {
    self.startTime = [NSDate date];
    [self performSelector:@selector(startIncrement) withObject:nil afterDelay:.3];
}


-(void)startIncrement {
    self.aTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(incr) userInfo:nil repeats:YES];
}

-(void)touchedUpOnTapper {
    if ([[NSDate date] timeIntervalSinceDate:self.startTime] < .3) 
        self.label.text = [NSString stringWithFormat:@"%d", self.label.text.integerValue +1];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self.aTimer invalidate];
}

-(void)incr{
    self.label.text = [NSString stringWithFormat:@"%d", self.label.text.integerValue +1];
}

因此,在正常的 touchDown/touchUp 序列中,这将增加 1,但如果您按住手指,则在稍有延迟后进行连续增加。

于 2013-07-18T06:27:53.540 回答
0

关于按住并不断增加,我不完全确定,但要增加文本标签,只需编辑文本属性。

-(IBAction) increase {
textLabel.text = [NSString stringWithFormat:@"%d", number];
}
于 2013-07-18T06:26:23.200 回答
0

您是否尝试过UIControlEventTouchDownUIControlEventAllTouchEvents控制 UIButton 的状态,它们可以帮助您实现您想要做的事情。

看看这个枚举

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
    UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
    UIControlEventTouchDragInside     = 1 <<  2,
    UIControlEventTouchDragOutside    = 1 <<  3,
    UIControlEventTouchDragEnter      = 1 <<  4,
    UIControlEventTouchDragExit       = 1 <<  5,
    UIControlEventTouchUpInside       = 1 <<  6,
    UIControlEventTouchUpOutside      = 1 <<  7,
    UIControlEventTouchCancel         = 1 <<  8,

    UIControlEventValueChanged        = 1 << 12,     // sliders, etc.

    UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
    UIControlEventEditingChanged      = 1 << 17,
    UIControlEventEditingDidEnd       = 1 << 18,
    UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing

    UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events
    UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField
    UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents           = 0xFFFFFFFF
};
于 2013-07-18T06:30:27.950 回答