- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想使用 UIKit Dynamics 组合 UIAttachmentBehavior 和 UICollisionBehavior,以便用户可以拖动 View (使用 UIPanGestureRecognizer
)但不会离开特定区域。
当用户/UIView 与碰撞行为的边界发生碰撞时,问题就出现了,因为此时不可能进行垂直移动。
即当与边界区域的左侧发生碰撞时,一个人会“卡住”在那里,不能上下移动,就在右边。将 UIView 拖回原来的路径。
非常感谢 UIDynamicItemBehavior 解决此问题的任何帮助(尝试了弹性、摩擦和阻力,但无济于事)。
最佳答案
我认为您以错误的方式实现了 UIPanGestureRecognizer
。试试下面的例子。只需创建一个新项目并将此代码粘贴到您的 ViewController 中。如果需要缩小可拖动区域,可以使用 dragView
函数。
import UIKit
class ViewController: UIViewController, UICollisionBehaviorDelegate {
var collision: UICollisionBehavior!
var animator: UIDynamicAnimator!
override func viewDidLoad() {
super.viewDidLoad()
let container = UIView(frame: CGRect(x: 30, y: 60, width: 300, height: 500))
self.view.addSubview(container)
container.backgroundColor = .gray
self.animator = UIDynamicAnimator(referenceView: container);
//self.animator.setValue(true, forKey: "debugEnabled")
let greenBox = UIView(frame: CGRect(x: 60, y: 240, width: 50, height: 50))
greenBox.backgroundColor = .green
container.addSubview(greenBox)
let blueBox = UIView(frame: CGRect(x: 10, y: 10, width: 50, height: 50))
blueBox.backgroundColor = .blue
container.addSubview(blueBox)
let redBox = UIView(frame: CGRect(x: 200, y: 300, width: 50, height: 50))
redBox.backgroundColor = .red
container.addSubview(redBox)
self.collision = UICollisionBehavior(items: [greenBox, blueBox, redBox])
self.collision.translatesReferenceBoundsIntoBoundary = true
self.collision.collisionMode = .everything
self.animator.addBehavior(self.collision)
self.collision.collisionDelegate = self
let c = UIDynamicItemBehavior(items: [redBox])
c.density = 10000
self.animator.addBehavior(c)
let noRotation = UIDynamicItemBehavior(items: [greenBox, blueBox])
noRotation.allowsRotation = false
self.animator.addBehavior(noRotation)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.dragView(_:)))
greenBox.isUserInteractionEnabled = true
greenBox.addGestureRecognizer(panGesture)
let panGesture2 = UIPanGestureRecognizer(target: self, action: #selector(self.dragView(_:)))
blueBox.isUserInteractionEnabled = true
blueBox.addGestureRecognizer(panGesture2)
}
@objc func dragView(_ sender:UIPanGestureRecognizer){
if let viewDrag = sender.view {
self.view.bringSubview(toFront: viewDrag)
let translation = sender.translation(in: self.view)
viewDrag.center = CGPoint(x: viewDrag.center.x + translation.x, y: viewDrag.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: self.view)
animator.updateItem(usingCurrentState: viewDrag)
}
}
}
关于ios - 结合 UIAttachmentBehavior 和 UICollisionBehavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30956577/
documentation UIAttachmentView 中的 length 属性如下: Use this property to adjust the attachment length, if
我已经向我的 tableviewcell 添加了一个水平的 UIPanGestureRecognizer 来平移单元格的 subview ,就像 ios7 邮件应用程序显示删除和更多选项的方式一样,这
我在使用 initWithItem:attachedToItem: 时遇到问题 初始化连接一个动态项的中心点到另一个动态项的中心点的附件行为。但是当我使用方法 pan 改变 topview 的中心点时
在使用 Xcode 5.1.1 在 iOS7 中使用新的物理引擎时,我编写了以下代码: #import "ZViewController.h" @interface ZViewController (
我在 IB 中设置了一个 UIScrollView,它具有包含 UIImageView 的自动布局。您可以查看 Evgenii 的 Github 存储库以了解其设置方式:https://github.
我想拖动一个 UIView,并让它移动附加到它的其他 View 的位置,就好像它们都是通过字符串附加的一样。起始状态将是一堆 UIViews 聚集在一起。如果你拉出一个,一旦它与被拖动的 View 之
我想使用 UIKit Dynamics 组合 UIAttachmentBehavior 和 UICollisionBehavior,以便用户可以拖动 View (使用 UIPanGestureReco
我有一个水平的 UICollectionView 和一个自定义的 UICollectionViewFlowLayout ,它在每个单元格上设置了一个 UIAttachmentBehavior 以在向左
在 UIButton 的子类中,我将 UIButton 附加到 UIAttachmentBehavior,它允许用户用手指在屏幕上拖动按钮。 在 - (void)touchesBegan:(NSSet
我刚从 Apple 开发者门户网站下载 DynamicsCatalog.xcodeproj,但出现此错误: “UIAttachmentBejavior”没有可见的@interface decleare
我是一名优秀的程序员,十分优秀!