开发者

UIButton Touch and Hold

开发者 https://www.devze.com 2023-01-08 11:52 出处:网络
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 a开发者_运维问答nd

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 a开发者_运维问答nd over until it gets released?


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

For example:

//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;

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];
}

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.

For example:

-(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.


Unfortunately, it still looks like you have to code this functionality for yourself. simplest way (You still need a timer though):

A function that performs the action you want to repeat:

-(void) actionToRepeat:(NSTimer *)timer
{
    NSLog(@"Action triggered");
}

in your .h file declare and set a property for a timer:

@interface ClassFoo
{
    NSTimer* holdTimer;
}

Then in the .m make two IBActions:

-(IBAction) startAction: (id)sender
{
    holdTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(actionToRepeat:) userInfo:nil repeats:YES];
    [holdTimer retain];
}

-(IBAction) stopAction: (id)sender
{
    [holdTimer invalidate];
    [holdTimer release];
    holdTimer = nil;
}

Then Just link to the Touch Down event in IB from the button to startAction and the Touch Up Inside to the 'Stop Action'. It isn't a one liner but it allows you to customise the rate the action repeats as well as allowing you to trigger it from another outlet/action.

You might consider subclassing UIButton and adding this functionality if you are going to be using this functionality often - then it is only (slightly) painful to implement the first time.


An other way to use this NBTouchAndHoldButton. This is exactly what you want, and very easy to implement it:

TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2];

Good luck!


I cannot reply to the first one, but this line:

timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];

for at least iOS 4.1 and newer needs to be:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];


I know this is an old question, but as an easy way, like to consider using "[NSObject performSelector:withObject:afterDelay:]" to repeatedly invoke methods in any particular time interval.

In this case:

NSTimeInterval someTimeInterval = 1;

- (IBAction)action:(id)sender {
    UIButton * const button = sender;
    if (button.state != UIControlStateHighlighted) {
        return;
    }
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender];
    [self performSelector:_cmd withObject:sender afterDelay:someTimeInterval];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消