【问题标题】:Detect Touch on Edge of the UIVIEW检测 UIVIEW 边缘的触摸
【发布时间】:2017-03-26 02:53:44
【问题描述】:

如何在 iOS Swift 中检测视图边缘或边框的触摸。当用户拖动 UIView 的边框时,我想增加 UIView 的宽度和高度。

【问题讨论】:

  • 你可能想看看下面的 SO 线程 - stackoverflow.com/questions/8460119/… 在你提问之前做一些搜索是值得的 :)
  • @ Fahim,我猜它实际上不是重复的,因为 OP 正在询问如何检测 UIView 边缘的触摸,这个问题解决了带有“句柄”的 UIView。也就是说,我建议 OP (1)看看这个问题,(2)考虑几乎所有其他 CocoaTouch 应用程序是如何做到这一点的——通过使用捏合手势。检测触摸对用户来说不是很友好... Apple 建议您的按钮最小尺寸为 40x40。你真的能期望鼠标指针产生的精确度吗?您应该使用手柄或捏合手势。
  • @ Fahim,该问题的解决方案是检测 UIView 角落的触摸,而我想检测边缘的触摸。
  • @dfd 这就是我想知道的,我是否需要添加 4 个不同的不可见视图来检测边缘的触摸,这是正确的视图还是有其他方法?

标签: ios swift


【解决方案1】:

得到解决方案修改How to resize UIView by dragging from its edges?解决方案。

用于检测边缘和角落的 Swift 版本和代码

class OverlayView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */


    static var kResizeThumbSize:CGFloat = 44.0
    private typealias `Self` = OverlayView

    var imageView = UIImageView()

    var isResizingLeftEdge:Bool = false
    var isResizingRightEdge:Bool = false
    var isResizingTopEdge:Bool = false
    var isResizingBottomEdge:Bool = false

    var isResizingBottomRightCorner:Bool = false
    var isResizingLeftCorner:Bool = false
    var isResizingRightCorner:Bool = false
    var isResizingBottomLeftCorner:Bool = false


        //Define your initialisers here

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)

            isResizingBottomRightCorner = (self.bounds.size.width - currentPoint.x < Self.kResizeThumbSize && self.bounds.size.height - currentPoint.y < Self.kResizeThumbSize);
           isResizingLeftCorner = (currentPoint.x < Self.kResizeThumbSize && currentPoint.y < Self.kResizeThumbSize);
            isResizingRightCorner = (self.bounds.size.width-currentPoint.x < Self.kResizeThumbSize && currentPoint.y < Self.kResizeThumbSize);
            isResizingBottomLeftCorner = (currentPoint.x < Self.kResizeThumbSize && self.bounds.size.height - currentPoint.y < Self.kResizeThumbSize);

            isResizingLeftEdge = (currentPoint.x < Self.kResizeThumbSize)
            isResizingTopEdge = (currentPoint.y < Self.kResizeThumbSize)
            isResizingRightEdge = (self.bounds.size.width - currentPoint.x < Self.kResizeThumbSize)

            isResizingBottomEdge = (self.bounds.size.height - currentPoint.y < Self.kResizeThumbSize)

            // do something with your currentPoint

        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)
            // do something with your currentPoint
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)
            // do something with your currentPoint


            isResizingLeftEdge = false
             isResizingRightEdge = false
             isResizingTopEdge = false
             isResizingBottomEdge = false

             isResizingBottomRightCorner = false
             isResizingLeftCorner = false
             isResizingRightCorner = false
             isResizingBottomLeftCorner = false

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2013-11-05
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多