我有一个风格化的UIButton
子类,我想在点击时显示标准的AirPlay路线选择界面。我知道AVRoutePickerView
可用,但它似乎没有提供任何自定义设置,只是在iOS上配置其色调和活动色调颜色。
是否可以使用您自己的按钮显示路线选择器?
此应用似乎可以做到这一点:
答案 0 :(得分:0)
由于AVRoutePickerView只是一个视图,您可以将其添加到自定义按钮或在其顶部添加自定义视图。这是一个非常粗糙的例子
let frame = CGRect(x: 50, y: 50, width: 100, height: 50)
let routePickerView = AVRoutePickerView(frame: frame)
routePickerView.backgroundColor = UIColor.clear
view.addSubview(routePickerView)
let label = UILabel(frame: routePickerView.bounds)
label.backgroundColor = .black
label.textColor = .white
routePickerView.addSubview(label)
label.text = "Rough"
我匆忙地举了个例子,说明如何使它起作用,按钮轻触传递非常敏感,并且尝试使用手势识别器。但是,我想出了三种替代方法,可以通过一些努力来使用。它们都依赖于将视图添加到选择器中作为上述子视图。该视图应具有.isUserInteractionEnabled = true
。我使用UILabel
和普通UIView
进行了测试,它可能需要没有触摸处理,因此请避免使用UIControl
。拥有此视图后,您可以使用touchesBegan
touchesCancelled
和touchesEnded
进行任何视觉调整或动画,然后进行触摸。与往常一样,摆弄自定义提供的控件可能并不总是有效,但这目前对我来说是有效的,并且不使用私有api只是解决方法和运气。
使用自定义UILabel作为子视图
class CustomLabel: UILabel {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
transform = .init(scaleX: 0.75, y: 0.75)
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
transform = .identity
super.touchesEnded(touches, with: event)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
transform = .identity
super.touchesCancelled(touches, with: event)
}
}
使用AVRoutePickerView的子类
class CustomRoutePicker: AVRoutePickerView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
transform = .init(scaleX: 0.75, y: 0.75)
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
transform = .identity
super.touchesEnded(touches, with: event)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
transform = .identity
super.touchesCancelled(touches, with: event)
}
}
在UIViewController中使用对pickerView的引用
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
pickerView?.transform = .init(scaleX: 0.75, y: 0.75)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
pickerView?.transform = .identity
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
pickerView?.transform = .identity
}