UIButton触摸并按住 [英] UIButton Touch and Hold

查看:116
本文介绍了UIButton触摸并按住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到一个非常容易的方法来做到这一点。我看到的方式需要所有这些计时器和东西。有没有任何简单的方法,我可以持有一个UIButton并导致它重复的动作一遍又一遍,直到它释放?

I haven't found a very easy way to do this. The ways I've seen require all these timers and stuff. Is there any easy way I can hold a UIButton and cause it to repeat the action over and over until it gets released?

推荐答案

您可以执行以下操作:创建一个NSTimer,它将在应用程序启动时启动,或在viewDidLoad中启动,并且还使用布尔值。

You can do the following: Make an NSTimer that will start up when the app starts or in viewDidLoad and also make a boolean.

例如:

//Declare the timer, boolean and the needed IBActions in interface.
@interface className {
NSTimer * timer;
bool g;
}
-(IBAction)theTouchDown(id)sender;
-(IBAction)theTouchUpInside(id)sender;
-(IBAction)theTouchUpOutside(id)sender;

//Give the timer properties.
@property (nonatomic, retain) NSTimer * timer;

现在在您的实施文件(.m)中:

Now in your implementation file (.m):

//Synthesize the timer
@synthesize timer;
//When your view loads initialize the timer and boolean.
-(void)viewDidLoad {
    g = false;
    timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
}



现在为Touch Down设置IBAction设置布尔值。然后为Touch Up Inside和Touch Up Outside另一个IBAction按钮分配布尔值为false。

Now make an IBAction for "Touch Down" set the boolean to lets say true. Then make another IBAction button for "Touch Up Inside" and "Touch Up Outside" assign the boolean to false.

例如:

-(IBAction)theTouchDown {
    g = true;
}

-(IBAction)theTouchUpInside {
    g = false;
}

-(IBAction)theTouchUpOutside {
    g = false;
}



您已声明)

Then in that NSTimer method, put the following:(assume g is the boolean you have declared)

-(void) targetmethod:(id)sender {
    if (g == true) {
        //This is for "Touch and Hold"
    }
    else {
        //This is for the person is off the button.
    }
}

我希望这简化了一切...我知道仍然使用计时器,但没有其他方法。

I hope this simplifies everything... I know it still uses a timer but there is not another way.

这篇关于UIButton触摸并按住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆