作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
您好,我想在我触摸已经选择的片段时获得事件。
我已经实现了以下解决方案
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/
我是一名优秀的程序员,十分优秀!