2017-11-09 4 views
2

を使用してのUIViewを移動それをしないでください。スケールと私は多くのソリューションを試しましたが、私ができる私は、ビューAのアイコンを置くのUIView Aを持っていると、このビューA.</p> <p>をスケーリングし、移動するためにパンジェスチャーを使用しようとUIPanGestureRecognizer

助けてください。

もっと詳しく:

私は、詳細を追加します。

私はビューAを持っており、セルフビューにサブを追加しています。そして私は、ビューAにpanGestureRegonizerを描画すると、ビューAは後続の描画を移動します。

移動中のビューAは縮尺されます。画面Aがスクリーンの上/左/下/右に移動し、画面の中心に移動したときに大きくなるようにスケーリングすると、表示Aは小さくなります。

+0

あなたはより詳細な情報を追加できますか? – Archmede

+0

詳細を追加しました。もう一度確認してください。 @Archmede –

答えて

2

vc - ViewControllerがあり、UIView Aのサブビューがvcであるとします。 UIPanGestureRecognizerAに追加できます。そして、アクションとしてごvcにpanGestureRegonizerをドラッグ:

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) { 
     //your code here 
} 

senderからは、アクションのviewlocationstateを確認することができます。 stateは、達成しようとしていることに応じて、場合によってはコードに影響を与える可能性があります。

次に、あなたはこれにアクションを変更する必要があります

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) { 
     UIView.animateKeyframes(withDuration: 0.1, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: { 
      let location = sender.location(in: sender.view?.superview) 
      sender.view?.center = location 
     }) 
    } 

ここsender.view?.superviewvc.viewに等しいです。このコードスニペットはパンのジェスチャーを検出してからAに移動し、A.centerがジェスチャーの位置に一致します。期間0.1は、動きに滑らかなアニメーション効果を与えることに注意してください。

これは、パンジェスチャーで「移動」機能を提供します。スケーリングのため

EDIT

ロジック:あなたはcenterxyでシステム(CS)を座標持っています。ユーザがパンジェスチャを使用するとき、ユーザはCS内に一連のポイントを生成する。だから私たちの仕事は、CSの中心とユーザーのポイントとの間の距離を測定することです。最も遠い距離にいるときは、スケーリングビューのスケールファクタを計算できます。

var center: CGPoint! //center of the CS 
let maxSize: CGSize = CGSize.init(width: 100, height: 100) // maximum size of our scaling view 
var maxLengthToCenter: CGFloat! //maximum distance from the center of the CS to the furthest point in the CS 

private func prepareForScaling() { 
    self.center = self.view.center //we set the center of our CS to equal the center of the VC's view 
    let frame = self.view.frame 
    //the furthest distance in the CS is the diagonal, and we calculate it using pythagoras theorem 
    self.maxLengthToCenter = (frame.width*frame.width + frame.height*frame.height).squareRoot() 
} 

その後、我々は機能をスケーリングするための我々のデータを準備してする機能私たちのセットアップを呼び出す必要があります - 私たちはviewDidLoadでこれを行うことができます。

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.prepareForScaling() 
} 

その後、我々はのスケールサイズを計算するためにヘルパー機能を必要としますユーザのパンジェスチャの現在の位置を画面上に表示する。

private func scaledSize(for location: CGPoint) -> CGSize { 
    //calculate location x,y differences from the center 
    let xDifference = location.x - self.center.x 
    let yDifference = location.y - self.center.y 

    //calculate the scale factor - note that this factor will be between 0.0(center) and 0.5(diagonal - furthest point)  
    //It is due our measurement - from center to view's edge. Consider multiplying this factor with your custom constant. 
    let scaleFactor = (xDifference*xDifference + yDifference*yDifference).squareRoot()/maxLengthToCenter 
    //create scaled size with maxSize and current scale factor 
    let scaledSize = CGSize.init(width: maxSize.width*(1-scaleFactor), height: maxSize.height*(1-scaleFactor)) 

    return scaledSize 
} 

そして最後に、我々はAのサイズを変更するために私たちのパンジェスチャーアクションを変更する必要がありますが:

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) { 
    UIView.animateKeyframes(withDuration: 0.1, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: { 

     let location = sender.location(in: sender.view?.superview) 

     sender.view?.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: self.scaledSize(for: location)) 

     sender.view?.center = location 
    }) 
} 
+0

ありがとうございました。 UIPanGestureRecognizerを使った移動ビューでは、うまくいきました。私はあなたの解決策と同じようにしています。私は規模に問題があります。私は詳細を追加して、もう一度チェックしてください。 –

+0

私は数分後にスケーリング機能を使って答えを更新します。 –

関連する問題

 関連する問題