簡體   English   中英

如何在swift 3中設置Pan Gesture Limit

[英]How To set Pan Gesture Limit in swift 3

我采取了兩個視圖,在那個視圖中我采取圖像視圖..顯示..貝爾

在此輸入圖像描述

我需要設置限制,圖像中心不要超出超視圖的一邊..

我正在嘗試該代碼,但一段時間不正常工作正常..

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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM