完成效果:
首先创建一个长按手势,并创建响应方法,在响应方法里面添加一个print方法,这样打开模拟器后就可以查看是否添加成功。
func setupLongPressGesture() {
view.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress)))
}
@objc func handleLongPress(gesture:UILongPressGestureRecognizer) {
print("Long Press")
}
接下来声明一个containerView用来当作弹窗view。
let iconsContainerView:UIView = {
let view = UIView()
view.backgroundColor = .red
view.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
return view
}()
在长按手势的响应方法中判断手势的状态,如果是began就把弹窗view添加为视图的子view,如果是结束状态则移除弹窗view。
@objc func handleLongPress(gesture:UILongPressGestureRecognizer) {
if gesture.state == .began {
view.addSubview(iconsContainerView)
} else if gesture.state == .ended {
iconsContainerView.removeFromSuperview()
}
}
接下来要把弹窗的位置根据每次长按位置的变化。用gesture.location得到长按点击的位置,然后根据长按的位置算出中间的X值和弹窗的Y值,将iconsContainerView的位置设置为算出来的X和Y值。
let pressedLocation = gesture.location(in: view)
let centeredX = (view.frame.width - iconsContainerView.frame.width) / 2
iconsContainerView.transform = CGAffineTransform(translationX: centeredX, y: pressedLocation.y - iconsContainerView.frame.height)
接下来在为弹窗View添加一个alpha 和位置变化的animation,UIView.animate的参数在之前的文章有解释过是什么意思。
iconsContainerView.transform = CGAffineTransform(translationX: centeredX, y: pressedLocation.y )
iconsContainerView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut) {
self.iconsContainerView.alpha = 1
self.iconsContainerView.transform = CGAffineTransform(translationX: centeredX, y: pressedLocation.y - self.iconsContainerView.frame.height)
}
began里面的操作太长了,可以把began里面的操作分离出来作为一个方法来调用。
func handleBegan(_ gesture: UILongPressGestureRecognizer) {
view.addSubview(iconsContainerView)
let pressedLocation = gesture.location(in: view)
let centeredX = (view.frame.width - iconsContainerView.frame.width) / 2
iconsContainerView.transform = CGAffineTransform(translationX: centeredX, y: pressedLocation.y )
iconsContainerView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut) {
self.iconsContainerView.alpha = 1
self.iconsContainerView.transform = CGAffineTransform(translationX: centeredX, y: pressedLocation.y - self.iconsContainerVi