
I add a view to self.view, and add a UIPangestureRecognizer to the view, move the view, but not fluency, I can feel not fluency,
this my code, thanks you help me
var redView:UIView = UIView();
var redFrame:CGRect = CGRect();
override func viewDidLoad() {
super.viewDidLoad()
redView = UIView.init(frame: CGRect.init(x: 0, y: 100, width: KWidth, height: 40))
redView.backgroundColor = UIColor.red
self.redFrame = redView.frame;
self.view.addSubview(redView);
let pan:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(moveFunction(pan:)))
self.redView.addGestureRecognizer(pan)
}
func moveFunction(pan:UIPanGestureRecognizer) {
let point:CGPoint = pan.translation(in: self.view)
pan.setTranslation(CGPoint.zero, in: self.view)
if pan.state == .changed{
print(point)
let redY:CGFloat = redView.frame.origin.y + point.y
redView.frame = CGRect.init(x: 0, y: Int(redY), width: KWidth, height: 40)
}
}
I would not recommend to reallocate the frame of the view every time the state is
.changed. Only update thecenterproperty of the gesture recognizer's view.On the other hand, if you think about real life, when everything starts or stops to move, they do not use the same speed, they speed up, and slow down before stopping eventually.
Therefore i would recommend to wrap the
centerpositioning in aUIView's animation block, with.curveEaseInOutoption.Add this method as the action of your
UIPanGestureRecognizer, and it should give a much smoother experience.