UIButton:当isHighlighted = true时,我只能通过滑动手指来调用一个函数 [英] UIButton: when isHighlighted = true, I can only call a function by swiping my finger

查看:26
本文介绍了UIButton:当isHighlighted = true时,我只能通过滑动手指来调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些你的丰富知识:)

Need some of your great knowledge :)

我在不使用 UIButton 对象的情况下编写了 UIButton 的子类 (CustomWideButton.swift).我这样做是因为它对于我需要创建的按钮类型更加灵活.

I wrote a subclass of UIButton (CustomWideButton.swift) without using a UIButton object. I did it this way because it's more flexible for the types of buttons I need to create.

有一个奇怪的事情,我不知道这是不是一种正常的行为.

There is a strange thing and I don't know if it is a normal behaviour.

var isHighlighted 在点击正常的按钮区域时被调用.所以如果我写这样的东西:

var isHighlighted is called when tapping the button area which is normal. So if I write something like this:

var isHighlighted {
    didSet {
      print("I am Highlighted")
      animateHighlight()
    }
}

我将在控制台中看到我被突出显示",但它不会调用 animateHighlight() 除非我开始在按钮上轻轻滑动手指.如果我这样做,那么 animateHighlight() 就会起作用.

I will see in the console "I am highlighted", but it won't call animateHighlight() unless I slightly start swiping my finger on the button. If I do so, then animateHighlight() works.

我在 BitBucket 上发布了一个示例项目,以便您更容易理解我面临的问题:https://bitbucket.org/stephaneDepoilly/stackohighlightedbutton

I posted a sample project on BitBucket so it'll be easier for you to understand the issue I'm facing: https://bitbucket.org/stephaneDepoilly/stackohighlightedbutton

zip 的直接链接:https://bitbucket.org/stephaneDepoilly/stackohighlightedbutton/获取/ee4fc398f475.zip

如果自定义按钮代码看起来过大,我很抱歉,这是因为我从我的项目中部分提取了它并且我们使用的是 MVVM.

I'm sorry if the custom button code looks overkill, it's because I partially extracted it from my project and we're using MVVM.

预先感谢您的帮助!

推荐答案

您的调试消息误导了您.我将您的 print 替换为:

Your debugging messages are misleading you. I replaced your print with:

 NSLog("isHighlighted on \(title) set to \(isHighlighted)")

我在 animateHighlight() 的第一行添加:

And I added as the first line in animateHighlight():

 NSLog("animateHighlight enter: triggered by isHighlighted on \(title) set to \(isHighlighted)")

我得到了:

2017-08-27 21:57:34.870 StackO_HighlightFunctionCall isHighlighted on [...LOGIN...] set to true
2017-08-27 21:57:34.871 StackO_HighlightFunctionCall animateHighlight enter: triggered by isHighlighted on [...LOGIN...] set to true
2017-08-27 21:57:35.000 StackO_HighlightFunctionCall isHighlighted on [...LOGIN...] set to false
2017-08-27 21:57:35.000 StackO_HighlightFunctionCall animateHighlight enter: triggered by isHighlighted on [...LOGIN...] set to false

所以:

1) animateHighlight 当你告诉它被调用时确实被调用了.使用 DebuggerForce!

1) animateHighlight is indeed being called when you tell it to be called. Use the DebuggerForce!

2) isHighlighted 状态在设置为 true 后非常迅速(0.13 秒)切换回 false.你预料到了吗?

2) isHighlighted state switches very rapidly (0.13 sec) back to false after being set to true. Did you expect that?

3) animateHighlight 有问题,不会产生任何可见的变化.如果您仔细跟踪代码,您会看到 setupUI() 在更新标签文本后立即被调用.这是因为您正在从 layoutSubviews() 调用 setupUI().layoutSubviews 在运行时一直被调用,当有关视图的任何变化可能影响子视图时.setupUI 应该仅在视图被添加到其父视图时被调用,例如在 viewDidLoad()

3) animateHighlight is buggy and doesn't produce any visible change. If you trace through your code carefully you'll see setupUI() gets called right after updating the label's text. This is because you are calling setupUI() from layoutSubviews(). layoutSubviews gets called all the time during runtime when anything about a view changes that could possibly affect subviews. setupUI should get called only when the view is added to its superview, e.g. at viewDidLoad()

滑动有效的原因是 layoutSubviews()延迟直到你完成滑动,因为它是 .touchUpInside 事件触发了点击动作,但 isHighlighted 是由隐藏的 UIButton .touchDownInside 处理触发的!

The reason the swipe works is that layoutSubviews() is delayed until you finish swiping, because it's the .touchUpInside event that triggers the tap action, but isHighlighted is triggered by hidden UIButton .touchDownInside handling!

这篇关于UIButton:当isHighlighted = true时,我只能通过滑动手指来调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆