(IOS Dev中的新手)这是我的主屏幕,这个屏幕是.ccbi文件,按钮是我在代码中链接并实现方法的图像。我正在做的是当我在负按钮情况下点击加号按钮值增加一个相同的时候。但每次我点击按钮增加/减少值。我希望当我按下按钮时,值会不断增加,直到我取消触摸按钮。我怎样才能做到这一点?我在某处读到可以使用CCLayer TouchBegin();但我找不到材料来帮助我理解机制。有什么建议?
答案 0 :(得分:3)
您可以使用计时器和两个IBAction来完成。
声明一个Timer和两个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
事件
答案 1 :(得分:1)
您可以使用“触地”触发的方法,而不是通常的“内部触摸”。该方法将启动一个计时器,它将以您想要的任何速率更新标签。你可以在触摸时使用另一种方法来阻止它。
编辑后:
如果您有图像而不是按钮,则必须使用手势识别器执行此操作。我创建了一个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];
}
ViewController.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,但如果您按住手指稍微延迟,则会连续递增。
答案 2 :(得分:0)
关于按住,不断递增,我不完全确定,但是增加文本标签只需编辑文本属性。
-(IBAction) increase {
textLabel.text = [NSString stringWithFormat:@"%d", number];
}
答案 3 :(得分:0)
您是否尝试过 UIControlEventTouchDown &amp; UIControlEventAllTouchEvents 控制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
};