gpt4 book ai didi

ios - 在已选择的段上点击时检测事件

转载 作者:可可西里 更新时间:2023-10-31 23:58:08 27 4
gpt4 key购买 nike

您好,我想在我触摸已经选择的片段时获得事件。

我已经实现了以下解决方案

import UIKit

class MARSSegmentController: UISegmentedControl {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let oldValue:NSInteger = self.selectedSegmentIndex
super.touchesBegan(touches, withEvent: event)
if oldValue == self.selectedSegmentIndex{
sendActionsForControlEvents(UIControlEvents.ValueChanged)
}
}
}

这工作正常,但如果我点击未选择的段,ValueChanged 事件会执行两次。

实现点击手势无效。意味着当我点击未选定的段时,它会在使用点击手势时显示旧的选定段。

如果有任何解决方案,请提出建议。

最佳答案

我意识到这是一个较旧的问题,但我遇到了同样的问题,我需要检测是否选择了现有段以及是否更改了所选段。

上面的解决方案很接近,但这是对我有用的。在 touchesBegan 上捕获现有的选定段索引,然后在 touchesEnded 中将旧值与当前选定的段索引进行比较。如果它们相同,我手动触发 .ValueChanged 事件。注意:这是 Swift 2.0 语法。

class SegmentedControlExistingSegmentTapped : UISegmentedControl
{
// captures existing selected segment on touchesBegan
var oldValue : Int!

override func touchesBegan( touches: Set<UITouch>, withEvent event: UIEvent? )
{
self.oldValue = self.selectedSegmentIndex
super.touchesBegan( touches , withEvent: event )
}

// This was the key to make it work as expected
override func touchesEnded( touches: Set<UITouch>, withEvent event: UIEvent? )
{
super.touchesEnded( touches , withEvent: event )

if self.oldValue == self.selectedSegmentIndex
{
sendActionsForControlEvents( .ValueChanged )
}
}
}

关于ios - 在已选择的段上点击时检测事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30237667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com