首页 > 解决方案 > 在点击时切换 UIViews 类

问题描述

UIViews我在 a 中有 5 个不同View Controller,我想通过更改其边框颜色仅突出显示最后一个被点击的。

我做了一个Custom Highlighting Class在设计方面封装所需效果的,并给每个 UIView aUITapGestureRecognizer及其#selector(toggleFunction). 这 5 个UIViews已经分配了一个类,所以基本上我需要将 更改Original ClassCustom Class,或者只是根据需要打开/关闭它.

我的问题是如何在 5 之间点击时打开和关闭此自定义类UIViews

标签: swiftuitapgesturerecognizer

解决方案


你可以试试

extension UIView {

    func addBorder(_ add:Bool) {

        self.layer.borderColor = add ? UIColor.red.cgColor : UIColor.green.cgColor

        self.layer.borderWidth = add ? 5 : 0

    }
}

@objc func tapped(_ v:UITapGestureRecognizer) {

   let currentView = v.view!

   allViews.forEach {

      $0.addBorder($0 == currentView)
   }

}

假设你有

var allViews = [UIView]()

推荐阅读