Saturday 30 January 2021

Swift - Doing Pan gesture recognition

 Steps: 

1. Create a instance to the pan gesture class

2. Create a function that performs gesture action

3. Add gesture recognition to the pan gesture instance


Example:


self.imageViews.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapAction(tap:))))

//Creating a function for pan action

    @objc func panAction(gesture:UIPanGestureRecognizer)

    {

        let translation = gesture.translation(in: view)

        

        guard let mazeedView = gesture.view else{

            return

        }

        mazeedView.center = CGPoint(x: mazeedView.center.x + translation.x, y: mazeedView.center.y + translation.y)

        

        gesture.setTranslation(.zero, in: view)

    }


No comments:

Post a Comment