按住 iPhone 按钮 3 秒会转到不同的视图

发布于 2024-10-06 17:37:12 字数 217 浏览 0 评论 0原文

我的应用程序中有一个按钮,按下该按钮会转到视图 但我需要,当按下 3 秒时,它会进入不同的视图,

就像当你在 Safari 上的 ipad 上时,按住 url,它会显示一个带有副本等的弹出窗口,

但当按下 3 秒时,我需要它其次它转向另一种观点......

希望这是有道理的,如果不理解我会更好地解释,

非常感谢! pd,还如何让它显示弹出式窗口? 干杯!

I have a button in my app that when pressed goes to a view
but I need that when pressed for 3 seconds it would go to a different view,

like when you are on ipad on safari and you keep pressed the url, and it shows a pop up with copy etc,

but I need that when pressed for 3 second it goes to another view...

hope this makes sense, I will explain better if not understood,

thank you so much!
pd, also how to make it show the pop up style window?
cheers!

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

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

发布评论

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

评论(3

只是在用心讲痛 2024-10-13 17:37:12

尝试在视图控制器中设置 NSTimer 属性。按下按钮后,创建计时器并将其分配给您的属性。您可以使用以下命令检测该时刻:

[button addTarget:self action:@selector(startHoldTimer) forControlEvents:UIControlEventTouchDown];

并使用以下命令进行分配:

-(void) startHoldTimer {
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(goToNewView:) userInfo:nil repeats:NO];
}

然后设置一个操作在取消的触摸或内部触摸上运行:

[button addTarget:self action:@selector(touchUp) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(cancelTimer) forControlEvents:UIControlEventTouchCancel];

//if timer fires, this method gets called
-(void) goToNewView {
    [self cancelTimer];
    [self loadSecondView];
}

// normal button press invalidates the timer, and loads the first view
-(void) touchUp {
    [self cancelTimer];
    [self loadFirstView];
}

//protected, just in case self.myTimer wasn't assigned
-(void) cancelTimer {
    if (self.myTimer != nil)
        if ([self.myTimer isValid]) {
            [self.myTimer invalidate];
    }
}

应该可以解决它!

Try setting an NSTimer property in your view controller. When the button's pressed, create the timer and assign it to your property. You can detect that moment with this:

[button addTarget:self action:@selector(startHoldTimer) forControlEvents:UIControlEventTouchDown];

and assign with this:

-(void) startHoldTimer {
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(goToNewView:) userInfo:nil repeats:NO];
}

Then set an action to run on a canceled touch, or a touch up inside:

[button addTarget:self action:@selector(touchUp) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(cancelTimer) forControlEvents:UIControlEventTouchCancel];

and

//if timer fires, this method gets called
-(void) goToNewView {
    [self cancelTimer];
    [self loadSecondView];
}

// normal button press invalidates the timer, and loads the first view
-(void) touchUp {
    [self cancelTimer];
    [self loadFirstView];
}

//protected, just in case self.myTimer wasn't assigned
-(void) cancelTimer {
    if (self.myTimer != nil)
        if ([self.myTimer isValid]) {
            [self.myTimer invalidate];
    }
}

That should take care of it!

白况 2024-10-13 17:37:12

使用 UILongPressGestureRecognizer,通过 -addGestureRecognizer: 添加到按钮 - 它将处理触摸计时并在识别到按钮已被按住一段时间时触发事件。不过,您可能需要重新考虑您的交互模式,通常,可以长按的不是按钮,它们是视图中的实际数据片段,例如 Safari 中的图像或链接。

Use a UILongPressGestureRecognizer, added to the button with -addGestureRecognizer:—it'll handle timing the touch and fire an event when it recognizes that the button's been held down for a while. You might want to reconsider your interaction pattern, though—generally, things that can be long-pressed aren't buttons, they're actual pieces of data in a view, like an image or a link in Safari.

哆兒滾 2024-10-13 17:37:12

一种可能的方法是实现一个触摸事件(我不记得名称,但是当您按下按钮时触发的方法),并安排一个计时器在三秒内触发。如果用户在此时间之前抬起手指,则取消计时器并执行正常的按钮单击。如果时间确实触发(即 3 秒过去),请忽略触摸事件并加载新视图。

One possible approach would be to implement one of the touches events (I don't remember the name, but the method that fires when you touch down on the button), and schedule a timer to fire in three seconds. If the user lifts her finger before that time, cancel the timer and perform the normal button click. If the time does fire (i.e. 3 seconds have elapsed), ignore the touch up event and load your new view.

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