How can I make a UI element added via -addSublayer: respond to touch events?

最后都变了- 提交于 2019-12-06 08:16:05

You can't. You'll have to add the button as a subview instead.

UIKit touch event forwarding is a feature of UIResponder, and CALayers don't inherit from UIResponder, so unless you fancy implementing touch handling from first principles this is not the way to go.

To convert your gradient layer to a view, there are two options:

1) Create a new UIView subclass and override its +(Class)layerClass method to return CAGradientLayer.

2) Create a regular UIView and add your CAGradientLayer as a sublayer, then add your button as a subview of the view instead of adding it to the CAGradientLayer directly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!