ios - 如何在swift 3中设置Pan Gesture Limit

标签 ios iphone swift

我采用两个 View ,在那个 View 中我采用 ImageView ..显示..下方

enter image description here

我需要设置限制,图像中心不会超出 super View 的一侧..

我正在尝试该代码,但有时无法正常工作..

func panImage(tap:UIPanGestureRecognizer) {
    let minx = (abs(Int((tap.view?.superview?.center.x)!)) - ((Int((tap.view?.superview?.frame.width)!))/2))
    let maxx = (abs(Int((tap.view?.superview?.center.x)!)) + ((Int((tap.view?.superview?.frame.width)!))/2))
    let miny = (abs(Int((tap.view?.superview?.center.y)!)) - ((Int((tap.view?.superview?.frame.height)!))/2))
    let maxy = (abs(Int((tap.view?.superview?.center.y)!)) + ((Int((tap.view?.superview?.frame.height)!))/2))
    let translation = tap.translation(in: tap.view)
    if(Int(tap.view!.center.x) > maxx)
    {tap.view!.center.x = CGFloat(maxx)}
    if(Int(tap.view!.center.x) < minx)
    {tap.view!.center.x = CGFloat(minx)}
    if(Int(tap.view!.center.y) > maxy)
    {tap.view!.center.y = CGFloat(maxy)}
    if(Int(tap.view!.center.y) < miny)
    {tap.view!.center.y = CGFloat(miny)}

    if((Int(tap.view!.center.x) >= minx && Int(tap.view!.center.x) <= maxx ) && (Int(tap.view!.center.y) <= maxy && Int(tap.view!.center.y) >= miny))
    {
        tap.view!.center = CGPoint(x: tap.view!.center.x + translation.x, y: tap.view!.center.y + translation.y)
        tap.setTranslation(CGPoint(x: 0.0, y: 0.0), in:tap.view)
    }
}

最佳答案

我正在尝试正常工作的代码..

这里

 func panImage(tap:UIPanGestureRecognizer) {
    let minx = 0
    let maxx = minx + Int((tap.view?.superview?.frame.size.width)!)
    let miny = 0
    let maxy = miny + Int((tap.view?.superview?.frame.size.height)!)
    let translation = tap.translation(in: tap.view)
    if(Int(tap.view!.center.x) > maxx)
    {tap.view!.center.x = CGFloat(maxx)}
    if(Int(tap.view!.center.x) < minx)
    {tap.view!.center.x = CGFloat(minx)}
    if(Int(tap.view!.center.y) > maxy)
    {tap.view!.center.y = CGFloat(maxy)}
    if(Int(tap.view!.center.y) < miny)
    {tap.view!.center.y = CGFloat(miny)}

    if((Int(tap.view!.center.x) >= minx && Int(tap.view!.center.x) <= maxx ) && (Int(tap.view!.center.y) <= maxy && Int(tap.view!.center.y) >= miny))
    {
        tap.view!.center = CGPoint(x: tap.view!.center.x + translation.x, y: tap.view!.center.y + translation.y)
        tap.setTranslation(CGPoint(x: 0.0, y: 0.0), in:tap.view)
    }
}

关于ios - 如何在swift 3中设置Pan Gesture Limit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855547/

相关文章:

ios - 检测用户何时离开 viewController

iphone - UITableView设计

ios - Fabric 请求不在存档或 iTunes Connect dSYM 文件中的 dSYM

ios - 当值长度== 1时,如何在值前添加 `0`

ios - 为什么 Xcode 中会有一个与我的常规项目分开的 'Pods' 项目?

iPhone - 本地化字符串无法在设备上运行

iphone - 链接器中的 Xcode 错误

ios - MKAnnotation Pin 不会拖动,即使设置为可拖动

ios - 对于第一次仅进入应用程序,在 swift 中使用 UIPageViewController 做教程页面的首选方法是什么

ios - "Reinitialize"共享实例