【问题标题】:Present AirPlay route picker from a custom UIButton从自定义 UIButton 呈现 AirPlay 路由选择器
【发布时间】:2019-10-11 08:01:18
【问题描述】:

我有一个程式化的UIButton 子类,我想在点击时呈现标准的 AirPlay 路由选择界面。我知道AVRoutePickerView 可用,但它似乎没有提供任何自定义,除了在 iOS 上配置其色调和活动色调颜色。

有没有办法使用您自己的按钮显示路线选择器?

看来这个应用程序能够做到这一点:

【问题讨论】:

    标签: ios airplay


    【解决方案1】:

    由于 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 进行了测试,它可能需要是没有触摸处理的东西,所以避免UIControls。一旦你有了这个视图,你就可以使用touchesBegantouchesCancelledtouchesEnded进行任何视觉调整或动画,然后传递触摸。与往常一样,在摆弄自定义提供的控件时,这可能并不总是有效,但这目前对我有用,并且不使用私有 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2019-01-24
      • 1970-01-01
      • 2013-10-25
      相关资源
      最近更新 更多