要让一个backgroundImageView成为可拖动的而不是主视图,可以通过以下步骤实现:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
backgroundImageView.addGestureRecognizer(panGesture)
@objc func handlePan(_ gesture: UIPanGestureRecognizer) {
let translation = gesture.translation(in: self.view)
backgroundImageView.center = CGPoint(x: backgroundImageView.center.x + translation.x, y: backgroundImageView.center.y + translation.y)
gesture.setTranslation(CGPoint.zero, in: self.view)
}
backgroundImageView.isUserInteractionEnabled = true
通过以上步骤,你可以让backgroundImageView成为可拖动的而不是主视图。用户可以通过拖动手势在父视图内自由移动backgroundImageView的位置。
领取专属 10元无门槛券
手把手带您无忧上云