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

查看:34
本文介绍了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,将布尔值设置为true.然后为内部触摸"和外部触摸"制作另一个 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;
}

然后在那个 NSTimer 方法中,输入以下内容:(假设 g 是您声明的布尔值)

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天全站免登陆