I'm trying to implement screen edge pan gesture for my view controller. But the problem is, if trying to add edge pan gesture for two edges (UIRectEdge.left, UIRectEdge.right) as
let screenEdgePanGesture = UIScreenEdgePanGestureRecognizer.init(target: self, action: #selector(self.didPanningScreen))
screenEdgePanGesture.edges = [.right, .left]
screenEdgePanGesture.delegate = self
self.view.addGestureRecognizer(screenEdgePanGesture)
the selector method not calling. But the edge pan gesture is working for one edge i.e.,
let screenEdgePanGesture = UIScreenEdgePanGestureRecognizer.init(target: self, action: #selector(self.didPanningScreen))
screenEdgePanGesture.edges = .right
screenEdgePanGesture.delegate = self
self.view.addGestureRecognizer(screenEdgePanGesture)
Yes, you are right, UIScreenEdgePanGestureRecognizer edges is accept/working only with one value, So you need to create two different functions for left and right edge panning.
Swift 4
let screenEdgePanGestureRight = UIScreenEdgePanGestureRecognizer.init(target: self, action: #selector(self.didPanningScreenRight(_:)))
screenEdgePanGestureRight.edges = .right
screenEdgePanGestureRight.delegate = self
self.view.addGestureRecognizer(screenEdgePanGestureRight)
let screenEdgePanGestureLeft = UIScreenEdgePanGestureRecognizer.init(target: self, action: #selector(self.didPanningScreenLeft(_:)))
screenEdgePanGestureLeft.edges = .left
screenEdgePanGestureLeft.delegate = self
self.view.addGestureRecognizer(screenEdgePanGestureLeft)
#objc func didPanningScreenRight(_ recognizer: UIScreenEdgePanGestureRecognizer) {
print("Right edge penning")
}
#objc func didPanningScreenLeft(_ recognizer: UIScreenEdgePanGestureRecognizer) {
print("Left edge penning")
}
Related
i have swipe gesture that switch between tab bars when I swipe in the screen right or left its change the tab bar im in it
How would I make it so it looks like it is sliding to the right or left tab bar rather than just instantly changing the tab bar
class SwipeGesture: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
left.direction = .left
self.view.addGestureRecognizer(left)
let right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
right.direction = .right
self.view.addGestureRecognizer(right)
}
#objc func swipeLeft() {
let total = self.tabBarController!.viewControllers!.count - 1
tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1)
}
#objc func swipeRight() {
tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
}
}
You can achieve this effect using this pod
If you want to build it from scratch, you need to subclass containerView. Then put the view controllers (childVCs) inside a scrollView (scrolls only in horizontal direction).
I have an array of image views.
var imageViewArray = [UIImageView(image: UIImage())]
I use a for loop to fill this array with images from urls. I want to make it so that when I touch one of these images it becomes hidden or alpha: 0. I tried this:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(StoryVC.imageTapped))
newImage.userInteractionEnabled = true
newImage.addGestureRecognizer(tapGestureRecognizer)
And I tried adding a tag too but I can't figure out how to get the sender. I need to be able to run the function to hide the image and know which image to hide, that is the part i'm struggling with. Thanks in advance.
You get UITapGestureRecognizer object in your selector's parameter and it has a property view that gives you the view which has been tapped.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))
func imageTapped(_ sender: UITapGestureRecognizer) {
guard let tappedImage = sender.view else { return }
}
The selector should be a function within your class. Here's an example:
// Setting up the tapGestureRecognizers
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(pressed:))
newImage.userInteractionEnabled = true
newImage.addGestureRecognizer(tapGestureRecognizer)
// The function that handles the tap event
func pressed(sender: UIImageView!) {
if sender.alpha == 0{
sender.alpha = 1
}
else{
self.alpha = 0
}
}
Also make sure to double check my syntax, I don't write with Swift often, so it may have some small errors.
I want to start pan gesture in 1 item. And at the instant the gesture is recognized I create a new item view at the exact same spot and transfer the pan gesture to the new item so the new hovering item moves with the fingers.
How can I do this?
Here is the code so far:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(useItemPanGesture(_:)))
item.addGestureRecognizer(panGesture)
func useItemPanGesture(panGesture: UIPanGestureRecognizer) {
let newView = createView()
newView.addPanGesture(target: self, action: #selector(self.actualPanGesture(panGesture)))
panGesture.enabled = false
}
func actualPanGesture(panGesture: UIPanGestureRecognizer) {
//do pan gesture stuff
}
I have trouble in the transfer of the pan gesture. Line starting with newView.addPangesture.
Is there a way to pickup all UIGestureRecognizer events in one method? (besides via directing all their selectors to the same method).
So for example:
// Add Gesture Recogniser (Long Press)
let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(GcMapView.longPressAction(_:)))
longPressGR.minimumPressDuration = 1
self.addGestureRecognizer(longPressGR)
// Add Gesture Recogniser (Pan)
let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GcMapView.panAction(_:)))
mapDragRecognizer.delegate = self
self.addGestureRecognizer(mapDragRecognizer)
// Add Gesture Recogniser (Pinch)
let pinchGestureRecogniser = UIPanGestureRecognizer(target: self, action: #selector(GcMapView.pinchAction(_:)))
pinchGestureRecogniser.delegate = self
self.addGestureRecognizer(pinchGestureRecogniser)
// SOME METHOD NOW TO PICKUP ALL EVENTS
func PICKUPALLEVENTS (sender:UIGestureRecognizer) {
print("(String(gestureRecognizer.dynamicType) - \(gestureRecognizer.state.hashValue) ")
}
No, I don't think there is any way to do that.
Have you tried adding a UIGestureRecognizer to your target, then checking the dynamic type of the UIGestureRecognizer in the selector called?
Is there a way to detect up & down swipes on TableView in a ViewController?
in ViewDidLoad();
let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeUp:"))
let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))
upSwipe.direction = .Up
downSwipe.direction = .Down
tableView.addGestureRecognizer(upSwipe)
tableView.addGestureRecognizer(downSwipe)
Unfortunately, this doesn't work.
func swipeDown() {
print("A")
}
nothing returned.
What is the appropriate way to detect gestures on TableView (not cell/as a whole)?
Implement UIScrollViewDelegate
Define scrollViewDidScroll(_:)
Set your tableview's scrollview's delegate to whatever object you've got the above defined in.