我构建了一个照片拼贴应用程序,并有我想添加手势识别器的UILabel和UIImageView元素。目前,我处理每种类型的PanGestures在两个不同的功能,因为我需要声明的panGesture.view的类型,无论是的UILabel或UIImageView的:如何在Swift中为UILabel和UIImageView类型分解panGestureHandling函数?
func handlePanGesture(panGesture: UIPanGestureRecognizer!) {
// Get translation
var translation = panGesture.translationInView(view)
panGesture.setTranslation(CGPointZero, inView: view)
panGesture.delegate = self
// Add dx,dy to current image position
var image = panGesture.view as! UIImageView
image.center = CGPoint(x: image.center.x + translation.x, y: image.center.y + translation.y)
}
func handlePanGestureText(panGesture: UIPanGestureRecognizer!) {
var translation = panGesture.translationInView(view)
panGesture.setTranslation(CGPointZero, inView: view)
panGesture.delegate = self
var label = panGesture.view as! UILabel
label.center = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)
}
是否有可能因式分解此代码,并把它所有的在一个函数中并将它用于这两种类型? (例如,如果UIPanGestureRecognizer = {的UILabel ...}
最佳,
最大