以编程方式动画(移动)UIView

时间:2018-02-02 00:31:43

标签: ios cocoa-touch uiview

想象一下你想要移动的很多兄弟视图(即20个兄弟视图)。 您只想更改其x坐标。 当用户在屏幕上滑动手指时,将实时定义移动。

通过改变它们的框架可以做到这一点吗?

view.frame.origin.x += 5;

还是性能太贵了?

是否有更高效的方式来移动所有这些观点?

1 个答案:

答案 0 :(得分:0)

那应该没问题。 Apple文档显示,使用UIGestureRecognizer并更新框架的中心是一种非常标准的移动视图的方式。

Apple documentation on Pan Gesture.

他们的例子与我看到它的使用情况有点不同,但一般概念是一样的。我个人会这样设计:

func viewDidLoad() {
    let panGesture = UIPanGestureRecognizer(target: self, action: Selector("didDrag:"))
    moveableView.addGestureRecognizer(panGesture)
}

func didDrag(gesture: UIPanGestureRecognizer) {
    let translation = gesture.translationInView(self.view)
    customView.center.x = customView.center.x + translation.x // Customize this.
    customView.center.y = customView.center.y + translation.y
}
相关问题