ios - 在已经设置了重力/碰撞/弹性的情况下在 Swift 中移动对象(UIView)

标签 ios swift3

我想在 swift 中创建一个从屏幕顶部开始并下降到屏幕底部并停留在那里的正方形。我还希望能够用我的手指将它拖到顶部并使其再次落到屏幕底部。关于如何做到这一点的任何提示?

我已经能够创建一个落在屏幕底部的正方形。我也可以用手指移动它。但问题是,每当我移动它时,正方形就停留在同一个地方。重力/碰撞/弹性在我移动后似乎不起作用。我希望它在我的手指离开正方形后掉落。请参阅下面的代码摘录。我可能想念一些简单的东西。任何提示将不胜感激。

import UIKit

class ViewController: UIViewController {

  var greenSquare: UIView?
  var redSquare:UIView?
  var animator: UIDynamicAnimator?

  override func viewDidLoad() {
    super.viewDidLoad()
    createFallingObject()

    func createFallingObject() {

     //remove square from superview
     redSquare?.removeFromSuperview()

     //create the shape
     var dimen = CGRect(x: 130,y: 25,width: 90,height: 90)
     redSquare = UIView(frame: dimen)
     redSquare?.backgroundColor = UIColor.red

     //then add to the screen
     self.view.addSubview(redSquare!)

     //Initialize the animator
     animator = UIDynamicAnimator(referenceView: self.view)

     //Add gravity to the squares
     let gravity = UIGravityBehavior(items: [redSquare!])
     let direction = CGVector(dx: 0.0, dy: 0.05)
     gravity.gravityDirection = direction

     //Collision
     let boundaries = UICollisionBehavior(items: [redSquare!])
     boundaries.translatesReferenceBoundsIntoBoundary = true

     //Elasticity
     let bounce = UIDynamicItemBehavior(items: [redSquare!])
     bounce.elasticity = 0.3



     // 1. create a gesture recognizer (tap gesture)
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))


     //add pan gesture Regcognizer to the red square
     let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
     redSquare?.addGestureRecognizer(panGestureRecognizer)


     animator?.addBehavior(boundaries)
     animator?.addBehavior(bounce)
     animator?.addBehavior(gravity)
    }

    //this method handle pan
    func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
     if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {

        let translation = gestureRecognizer.translation(in: self.view)
        // note: 'view' is optional and need to be unwrapped
        gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
        gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)

        //redSquare?.frame.origin.x = translation.x
        //redSquare?.frame.origin.y = translation.y
     }
   }

   // 3. this method is called when a tap is recognized
   func handleTap(sender: UITapGestureRecognizer) {
    createFallingObject()
   }
}

最佳答案

您应该在平移手势开始时从 UIDynamicAnimator 中删除项目,并在结束时将其添加回来。

当 View 被添加到动态动画师时,它的位置应该由动画师单独确定。从动画器中移除项目将允许 View 使用其框架正常定位。将它添加回动画器将导致动画器从当前状态恢复定位。

或者,如果您需要手动更新项目的位置(即不基于动画师的物理特性),您应该调用 animator.updateItem(usingCurrentState: myDynamicItem),其中 myDynamicItem 是您要更新的项目。

关于ios - 在已经设置了重力/碰撞/弹性的情况下在 Swift 中移动对象(UIView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46919158/

相关文章:

iphone - 在不是 ICarousel 中的 subview 的标签中显示 iCarousel 的当前索引

swift - 从 1 viewcontroller 转到另一个

ios - 如何以编程方式为 UIBarbutton 项目设置动画

swift3 - Swift 中的 Error 和 NSError 有什么区别?

iOS Swift - 调度组,异步调度

ios - 我可以在 GeoFire 查询上应用分页以减少加载时间吗

ios - 禁用/隐藏一个 View Controller 的 Root View Controller

ios - 如何从对象列表创建 JSON 字符串?

ios - 直接从任何地方呈现深度嵌套的 View Controller ,同时保留层次结构

ios - Firebase InvalidPathValidation hasChild 在 Swift 中