ios - 如何更改 UIview 的边框颜色?

标签 ios swift uiview

我想在按下时更改 uiview 的边框颜色,并在释放后返回正常的边框颜色。这样做的最佳做法是什么?

类似于下图: enter image description here

最佳答案

不要使用 UITapGestureRecognizer。您可以使用 UILongPressGestureRecognizer

代码:

   override func viewDidLoad() {
    super.viewDidLoad()

    let view = UIView.init(frame: CGRect.init(x: 30, y: 200, width: 100, height: 40))
    self.view.addSubview(view)
    view.layer.borderColor = UIColor.black.cgColor
    view.layer.borderWidth = 3
    let tapForView = UILongPressGestureRecognizer(target: self, action: #selector(self.toChangeColor(recognizer:)))
    tapForView.minimumPressDuration = 0.01
    view.isUserInteractionEnabled = true
    view.addGestureRecognizer(tapForView)
}

@objc func toChangeColor(recognizer:UILongPressGestureRecognizer)
{
    // Apply logic for changing background color.
    let view = recognizer.view
    if recognizer.state == .began {
        view?.layer.borderColor = UIColor.orange.cgColor
        print("view began")
    }
    else if recognizer.state == .ended {
        view?.layer.borderColor = UIColor.black.cgColor
        print("view ended")

    }

}

这将完美地工作。

关于ios - 如何更改 UIview 的边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47789444/

相关文章:

iOS swift 如何将 imageview 准确地放置在 uiview 中

ios - 应用程序连接错误 : AppConnect cannot be instantiated directly

ios - 滚动时保持 UITableViewCells 的选定状态

ios - 如果我调用 cancelAllOpeartions,OperationQueue 不会从队列中删除操作

swift - 对查询字符串非常困惑

ios - UIView.animate 没有为我的 UIVisualEffectView 设置动画

ios - Fitbit 身份验证的 oAuth.io 错误

ios - Storyboard需要更多时间来加载

arrays - 在 Collection View 中使用来自转义闭包的数组

ios - 我如何继续旋转图像,直到服务器快速响应