Answer the question
In order to leave comments, you need to log in
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? CenterViewController.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
}
}
}
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
You can see how this is already implemented - for example, in JASidePanel a) and b)
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldReceiveTouch touch: UITouch) -> Bool
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question