如何正确子类化 UIControl?
我不需要 UIButton
或类似的东西。 我想直接子类化 UIControl
并制作我自己的、非常特殊的控件。
但由于某种原因,我重写的任何方法都没有被调用。 目标操作的东西起作用了,目标接收到适当的操作消息。 然而,在我的 UIControl 子类中,我必须捕获触摸坐标,而这样做的唯一方法似乎是覆盖这些家伙:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"begin touch track");
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"continue touch track");
return YES;
}
即使 UIControl
使用 UIView
、initWithFrame:
中的指定初始值设定项进行实例化。
我能找到的所有示例始终使用 UIButton
或 UISlider
作为子类化的基础,但我想更接近 UIControl
因为那是我想要什么:快速且无延迟的触摸坐标。
I don't want UIButton
or anything like that. I want to subclass UIControl
directly and make my own, very special control.
But for some reason, none of any methods I override get ever called. The target-action stuff works, and the targets receive appropriate action messages. However, inside my UIControl
subclass I have to catch touch coordinates, and the only way to do so seems to be overriding these guys:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"begin touch track");
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"continue touch track");
return YES;
}
They get never called, even though the UIControl
is instantiated with the designates initializer from UIView
, initWithFrame:
.
All examples I can find always use a UIButton
or UISlider
as base for subclassing, but I want to go closer to UIControl
since that's the source for what I want: Fast and undelayed Touch coordinates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我知道这个问题很古老,但我也遇到了同样的问题,我想我应该给我 2 美分。
如果您的控件有任何子视图,则
beginTrackingWithTouch
、touchesBegan
等可能不会被调用,因为这些子视图正在吞噬触摸事件。如果您不希望这些子视图处理触摸,可以将
userInteractionEnabled
设置为NO
,这样子视图就会简单地传递事件。 然后您可以覆盖touchesBegan/touchesEnded
并在那里管理所有触摸。希望这可以帮助。
I know this question is ancient, but I had the same problem and I thought I should give my 2 cents.
If your control has any subviews at all,
beginTrackingWithTouch
,touchesBegan
, etc might not get called because those subviews are swallowing the touch events.If you don't want those subviews to handle touches, you can set
userInteractionEnabled
toNO
, so the subviews simply passes the event through. Then you can overridetouchesBegan/touchesEnded
and manage all your touches there.Hope this helps.
Swift
这些是子类
UIControl
的不止一种方法。 当父视图需要对触摸事件做出反应或从控件获取其他数据时,通常使用 (1) 目标或 (2) 具有覆盖触摸事件的委托模式来完成。 为了完整起见,我还将展示如何 (3) 使用手势识别器执行相同的操作。 其中每种方法的行为都类似于以下动画:您只需选择以下方法之一。
方法 1:添加目标
UIControl
子类支持已内置的目标。如果您不需要向父级传递大量数据,这可能就是您想要的方法。MyCustomControl.swift
ViewController.swift
注释
TouchDragInside
事件提供帮助。传递其他数据
如果您希望在目标操作方法中访问自定义控件中的某些值
,则可以传入对该控件的引用。 设置目标时,请在操作方法名称后添加冒号。 例如:
请注意,它是
touchedDown:
(带冒号),而不是touchedDown
(不带冒号)。 冒号表示参数正在传递给操作方法。 在操作方法中,指定参数是对UIControl
子类的引用。 有了该参考,您就可以从控件中获取数据。方法 2:委托模式并覆盖触摸事件
通过子类化
UIControl
,我们可以访问以下方法:beginTrackingWithTouch
在手指首次在控件边界内触碰时调用。endTrackingWithTouch
被调用。如果您需要对触摸事件进行特殊控制,或者您需要与父级进行大量数据通信,那么此方法可能比添加目标效果更好。
下面是如何做到这一点:
MyCustomControl.swift
ViewController.swift
这就是视图控制器如何设置为委托并响应来自我们的自定义控件的触摸事件。
注释
如果这些方法仅在自定义控件本身中使用,则无需将委托与这些方法一起使用。 我本可以添加一个
print
语句来显示事件是如何被调用的。 在这种情况下,代码将简化为方法 3:使用手势识别器
添加手势识别器可以在任何视图上完成,并且它也适用于
UIControl
。 为了获得与顶部示例类似的结果,我们将使用UIPanGestureRecognizer
。 然后,通过测试事件触发时的各种状态,我们可以确定发生了什么。MyCustomControl.swift
ViewController.swift
注释
action: "gestureRecognized: “
。 冒号表示正在传入参数。Swift
These is more than one way to subclass
UIControl
. When a parent view needs to react to touch events or get other data from the control, this is usually done using (1) targets or (2) the delegate pattern with overridden touch events. For completeness I will also show how to (3) do the same thing with a gesture recognizer. Each of these methods will behave like the following animation:You only need to choose one of the following methods.
Method 1: Add a Target
A
UIControl
subclass has support for targets already built in. If you don't need to pass a lot of data to the parent, this is probably the method you want.MyCustomControl.swift
ViewController.swift
Notes
didDragInsideControl:withEvent:
means that two parameters are being passed to thedidDragInsideControl
method. If you forget to add a colon or if you don't have the correct number of parameters, you will get a crash.TouchDragInside
event.Passing other data
If you have some value in your custom control
that you want to access in the target action method, then you can pass in a reference to the control. When you are setting the target, add a colon after the action method name. For example:
Notice that it is
touchedDown:
(with a colon) and nottouchedDown
(without a colon). The colon means that a parameter is being passed to the action method. In the action method, specify that the parameter is a reference to yourUIControl
subclass. With that reference, you can get data from your control.Method 2: Delegate Pattern and Override Touch Events
Subclassing
UIControl
gives us access to the following methods:beginTrackingWithTouch
is called when the finger first touches down within the control's bounds.continueTrackingWithTouch
is called repeatedly as the finger slides across the control and even outside of the control's bounds.endTrackingWithTouch
is called when the finger lifts off the screen.If you need special control of the touch events or if you have a lot of data communication to do with the parent, then this method may work better then adding targets.
Here is how to do it:
MyCustomControl.swift
ViewController.swift
This is how the view controller is set up to be the delegate and respond to touch events from our custom control.
Notes
It is not necessary to use a delegate with these methods if they are only being used within the custom control itself. I could have just added a
print
statement to show how the events are being called. In that case, the code would be simplified toMethod 3: Use a Gesture Recognizer
Adding a gesture recognizer can be done on any view and it also works on a
UIControl
. To get similar results to the example at the top, we will use aUIPanGestureRecognizer
. Then by testing the various states when an event is fired we can determine what is happening.MyCustomControl.swift
ViewController.swift
Notes
action: "gestureRecognized:"
. The colon means that parameters are being passed in.我花了很长时间努力寻找解决这个问题的方法,但我认为没有。 但是,仔细检查文档后,我认为应该调用
begintrackingWithTouch:withEvent:
和continueTrackingWithTouch:withEvent:
可能是一种误解...< code>UIControl 文档说:
其中的关键部分(在我看来不是很清楚)是它说您可能想要扩展
UIControl
子类 - 而不是您可能想要直接扩展UIControl
。 可能不应该调用beginTrackingWithTouch:withEvent:
和continuetrackingWithTouch:withEvent:
来响应触摸,而应该调用UIControl
直接子类调用它们,以便它们的子类可以监视跟踪。因此,我的解决方案是重写
touchesBegan:withEvent:
和touchesMoved:withEvent:
并从那里调用它们,如下所示。 请注意,此控件未启用多点触摸,并且我不关心触摸结束和触摸取消事件,但如果您想完整/彻底,您可能也应该实现这些事件。请注意,您还应该使用
sendActionsForControlEvents:
发送与您的控件相关的任何UIControlEvent*
消息 - 这些消息可能会从超级方法中调用,我还没有测试过。I have looked long and hard for a solution to this problem and I don't think there is one. However, on closer inspection of the documentation I think it might be a misunderstanding that
begintrackingWithTouch:withEvent:
andcontinueTrackingWithTouch:withEvent:
are supposed to be called at all...UIControl
documentation says:The critical part of this, which is not very clear in my view, is that it says you may want to extend a
UIControl
subclass - NOT you may want to extendUIControl
directly. It's possible thatbeginTrackingWithTouch:withEvent:
andcontinuetrackingWithTouch:withEvent:
are not supposed to get called in response to touches and thatUIControl
direct subclasses are supposed to call them so that their subclasses can monitor tracking.So my solution to this is to override
touchesBegan:withEvent:
andtouchesMoved:withEvent:
and call them from there as follows. Note that multi-touch is not enabled for this control and that I don't care about touches ended and touches canceled events, but if you want to be complete/thorough you should probably implement those too.Note that you should also send any
UIControlEvent*
messages that are relevant for your control usingsendActionsForControlEvents:
- these may be called from the super methods, I haven't tested it.我认为您忘记添加 [super] 调用 TouchesBegan/touchesEnded/touchesMoved。
如果您像这样重写 TouchBegan / TouchEnded 之类的方法
将不起作用:
但是! 如果方法如下,则一切正常:
I think that you forgot to add [super] calls to touchesBegan/touchesEnded/touchesMoved.
Methods like
aren't working if you overriding touchesBegan / touchesEnded like this :
But! All works fine if methods will be like :
我经常使用的最简单的方法是扩展 UIControl,但利用继承的 addTarget 方法来接收各种事件的回调。 关键是监听发送者和事件,以便您可以找到有关实际事件的更多信息(例如事件发生的位置)。
因此,只需简单地子类化 UIControl,然后在 init 方法中(如果使用 nib,请确保还设置了 initWithCoder),添加以下内容:
当然,您可以选择任何标准控件事件,包括 UIControlEventAllTouchEvents。 请注意,选择器将传递两个对象。 首先是控制。 第二个是有关事件的信息。 下面是使用触摸事件根据用户是否按下左侧和右侧按钮来切换按钮的示例。
当然,这适用于非常简单的控件,您可能会达到这样的程度,这不会为您提供足够的功能,但这已经满足了我对自定义控件的所有目的,并且非常易于使用,因为我通常关心相同的内容UIControl 已支持的控件事件(触摸、拖动等...)
此处自定义控件的代码示例:自定义 UISwitch (注意:这不会向 buttonPressed:forEvent: 选择器注册,但您可以从上面的代码中看出)
The simplest approach that I often use is to extend UIControl, but to make use of the inherited addTarget method to receive callbacks for the various events. The key is to listen for both the sender and the event so that you can find out more information about the actual event (such as the location of where the event occurred).
So, just simply subclass UIControl and then in the init method (make sure your initWithCoder is also setup if you are using nibs) , add the following:
Of course, you can choose any of the standard control events including UIControlEventAllTouchEvents. Notice that the selector will get passed two objects. The first is the control. The second is the info about the event. Here's an example of using the touch event to toggle a button depending on if the user pressed on the left and right.
Granted, this is for pretty simplistic controls and you may reach a point where this will not give you enough functionality, but this has worked for all my purposes for custom controls to this point and is really easy to use since I typically care about the same control events that the UIControl already supports (touch up, drag, etc...)
Code sample of custom control here: Custom UISwitch (note: this does not register with the buttonPressed:forEvent: selector, but you can figure that out from the code above)
我遇到了 UIControl 无法响应
beginTrackingWithTouch
和continueTrackingWithTouch
的问题。我发现我的问题是当我执行
initWithFrame:CGRectMake()
时,我将框架做得很小(反应区域),并且只有几个点可以工作。 我将框架设置为与控件相同的大小,然后每当我按下控件中的任何位置时,它都会做出响应。I was having trouble with a UIControl not responding to
beginTrackingWithTouch
andcontinueTrackingWithTouch
.I found my problem was when I did
initWithFrame:CGRectMake()
I made the frame to small (the area that reacts) and had only a couple point spot where it did work. I made the frame the same size as the control and then anytime I pressed on anywhere in the control it responded.Obj C
我找到了 2014 年的相关文章 https://www.objc.io/issues /13-架构/行为/。
有趣的是,它的方法是利用 IB 并将事件处理逻辑封装在指定对象内(他们称之为行为),从而从视图/视图控制器中删除逻辑,使其更轻。
Obj C
I found a related article from 2014 https://www.objc.io/issues/13-architecture/behaviors/.
The interesting thing about it is that its approach is to make use of IB and encapsulate the event handling logic inside a designated object (they call it behaviors), thus removing logic from your view/viewController making it lighter.