开发者

Implement Long press on button and show delete button on top of that button like deleting app from background

I want to implement a long press on my button and after that long press a cross button should appear on the top of the button to remove that button from scroll view which is added in table view.

I search on web and got UILongPressGestureRecognizer to implement.I tried with it but I have many button in my scroll view and I put UILongPressGestureRecognizer on every button but how will I give reference of the pressed button to the selector method so that I can add a cross button on that p开发者_JS百科articular pressed button.

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteAppFromList:)];
UIButton *btn=(UIButton *)[cell.contentView viewWithTag:101+i];
[btn addGestureRecognizer:longPressGesture];
[longPressGesture release];

Please suggest me how to implement this.I want to do the functionality just as when we delete an app from ios device or simulator.


I've just done something similar. I detect the touchDown event, schedule a timed method which sets a flag after 1 second, then when the touch up event is detected it checks the flag and calls the appropriate method.

[myButton addTarget:self action:@selector(itemTouchDown) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(itemTouchUpInside) forControlEvents:UIControlEventTouchUpInside];

-(void)itemHoldTimer:(NSTimer *)timer
{
    self.itemHoldTimer = nil;
    didHold = YES;
}

-(void)itemTouchDown{
    didHold = NO;
    self.itemHoldTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(itemHoldTimer:) userInfo:nil repeats:NO]; 
}

-(void)itemTouchUpInside {
    if (didHold) {
        didHold = NO;
        [self itemWasTouchedUpAndDidHold];
    } else {
        didHold = NO;
        [self itemWasTouchedUp];
    }
}


In your handler method that you set up as the target of the gesture recognizer you are passed a reference to the recognizer that fired, e.g.:

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

The recognizer has a 'view' property, which is the view the recognizer is attached to. Use this directly or get the tag of this view and you can work out which button was pressed.

UIView *myButton = gestureRecognizer.view;

Having said that, adding a gesture recognizer to each button seems the wrong way to do this. I would create a custom control and handle 'touchesBegan' and 'touchesEnded' directly.


In the handler method of the long press recognizer, use the view property and its tag to identify the view.

-(void)handleLongPress:(UILongPressGestureRecognizer*)longPressRecognizer 
{
    //longPressRecognizer.view.tag
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜