实现长按按钮并在该按钮顶部显示删除按钮,就像从后台删除应用程序一样

发布于 2024-12-08 04:23:18 字数 601 浏览 0 评论 0原文

我想实现长按按钮,长按后按钮顶部应出现一个十字按钮,以从添加到表视图中的滚动视图中删除该按钮。

我在网上搜索并实现了 UILongPressGestureRecognizer 。我尝试过它,但我的滚动视图中有很多按钮,我在每个按钮上放置了 UILongPressGestureRecognizer 但我将如何提供参考将按下的按钮添加到选择器方法,以便我可以在该特定按下的按钮上添加十字按钮。

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

请建议我如何实现这一点。我想要执行该功能,就像我们从 ios 设备或模拟器中删除应用程序一样。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尾戒 2024-12-15 04:23:18

我刚刚做了类似的事情。我检测到 touchDown 事件,安排一个定时方法,在 1 秒后设置一个标志,然后当检测到 touch up 事件时,它检查该标志并调用适当的方法。

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

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];
    }
}
一张白纸 2024-12-15 04:23:18

在设置为手势识别器目标的处理程序方法中,您将传递对触发的识别器的引用,例如:

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

识别器有一个“view”属性,这是识别器附加到的视图。直接使用它或获取该视图的标签,您可以找出按下了哪个按钮。

UIView *myButton = gestureRecognizer.view;

话虽如此,为每个按钮添加手势识别器似乎是错误的方法。我将创建一个自定义控件并直接处理“touchesBegan”和“touchesEnded”。

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.

风和你 2024-12-15 04:23:18

在长按识别器的处理程序方法中,使用 view 属性及其标签来识别视图。

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文