0
當我使用'UIPanGestureRecognizer'移動'UIImageView'對象時,我注意到'center'屬性沒有改變。爲什麼是這樣,我做錯了什麼?以下是代碼:爲什麼用UIPanGestureRecognizer(Swift)平移UIImageView.center不會改變?
func handlePanning1(recognizer: UIPanGestureRecognizer)
{
var index: Int = recognizer.view!.tag - 1 // index in the arrays for this piece
var newTranslation: CGPoint = recognizer.translationInView(pieces[index])
recognizer.view?.transform = CGAffineTransformMakeTranslation(lastTranslations[index].x + newTranslation.x, lastTranslations[index].y + newTranslation.y)
// THIS ALWAYS PRINTS OUT THE SAME WHILE I'M PANNING
// AND IF I PAN MULTIPLE TIMES IN DIFFERENT DIRECTIONS (AKA IT NEVER CHANGES)
print(Int(pieces[index].center.x))
print("\n")
if recognizer.state == UIGestureRecognizerState.Ended {
lastTranslations[index].x += newTranslation.x
lastTranslations[index].y += newTranslation.y
}
}
哦,好的,這很有道理。因此,在使用「UIPanGestureRecognizer」進行平移時,最好是應用轉換還是實際移動它(通過更改「中心」)? – 2014-10-31 19:20:55
取決於你想完成什麼。我還沒有考慮哪個更有效率,我會假設實際上移動它,但這只是一個假設。如果您依靠其中心來更新位置,請繼續並更新中心。 – 2014-10-31 19:27:42