V
V
vii_onikovich2016-04-18 23:48:36
iOS
vii_onikovich, 2016-04-18 23:48:36

Who can help with UIPanGestureRecognizer?

I set myself a goal to write a side menu without frames. Almost did it, but the problem with moving to the right side, how to completely disable the opening to the right side? 7257f2637f6b4c78bc831d41cd9c9713.png0235c241894049eeb3568a0a94df0f79.png870bf06e9a6d49788d60be6ab13f898d.pngCenterViewController.swift

import UIKit

class CenterViewController: UIViewController {

    override func viewDidLoad() {
        
        super.viewDidLoad()

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(CenterViewController.handlePanGesture(_:)))
        self.view.addGestureRecognizer(panGestureRecognizer)
        
    }

    enum SideMenuControllerPresentationStyle {
        
        case None
        case Started
        case Ended
 
    }
    
    var presentationStyle: SideMenuControllerPresentationStyle = .None

    func handlePanGesture(recognizer: UIPanGestureRecognizer) {
        
        let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)

        switch(recognizer.state) {
            
            case .Began:
                
                if gestureIsDraggingFromLeftToRight {
                
                    self.view.layer.shadowColor = UIColor.lightGrayColor().CGColor
                    self.view.layer.shadowOpacity = 1
                    self.view.layer.shadowOffset = CGSizeZero
                    self.view.layer.shadowRadius = 10
                    
                    presentationStyle = .Started
                    
                }
            
                if presentationStyle == .Ended {
                    
                    presentationStyle = .Started
                    
                }
            
            case .Changed:

                if presentationStyle == .Started {
 
                    recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
                    recognizer.setTranslation(CGPointZero, inView: view)

                    
                }

            case .Ended:
            
                if self.view.frame.origin.x > 125 {
                    
                
                    UIView.animateWithDuration(0.2, delay: 0, options: [], animations: {
                    
                    self.view.frame.origin.x = 250
                    
                        }, completion: { (Bool) -> Void in
 
                    })
                    
                    self.presentationStyle = .Ended
            
                } else {
                    
                    UIView.animateWithDuration(0.2, delay: 0, options: [], animations: {
                        
                        self.view.frame.origin.x = 0
                        
                        }, completion: { (Bool) -> Void in
                            
                        
                            
                    })
                    
                    self.presentationStyle = .None
                    
                }
            
            default:
                
                break
            
            
        }
        
    }

}

ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let centerController = self.storyboard?.instantiateViewControllerWithIdentifier("CenterViewController") as! CenterViewController
        
        centerController.view.frame = self.view.frame
        
        addChildViewController(centerController)
        
        self.view.addSubview(centerController.view)
        
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Marunko, 2016-04-19
@Antonio-banderas

You can see how this is already implemented - for example, in JASidePanel a) and b)

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
             shouldReceiveTouch touch: UITouch) -> Bool

You can try to use this delegate method and limit the perception of the corresponding gestures based on points, if I understand correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question