[英]Animating (moving) a UIView programmatically
想象一下,有很多想要移動的同級視圖(即20個同級視圖)。 您只想更改其x坐標。 當用戶在屏幕上滑動手指時,將實時定義移動。
這樣改變框架是否可以做到這一點?
view.frame.origin.x += 5;
還是在性能上過於昂貴?
有沒有更有效的方式來移動所有這些視圖?
那應該沒問題。 蘋果文檔顯示,使用UIGestureRecognizer
並更新框架的中心是移動視圖的一種非常標准的方法。
他們的示例似乎與我所使用的示例有所不同,但是總體概念是相同的。 我將這樣設計:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.