Detecting UIButton tap for button inside of tappable view - swift

I have a container view with a button over it which hides and shows the view. Within the shown view, there are N number of mini buttons that have actions.
The problem I'm having is, when I tap on the mini buttons, those targets are ignored and the larger view button is what receives the action.
How do I configure things so that the larger tappable button on the view still works in most places but where the mini buttons exist, those tap actions register as well?
Thanks!

There are two possible solution
First
Change view hierarchy of uibutton (large on top of the stack in
interface builder)
Like
-Largebutton
-minibutton1
-minibutton1
'
'
-minibuttonn
Second one
Use gesture on the conainer view like
let hideViewGesture = UITapGestureRecognizer(target: self, action: "hideView")
containerView.addGestureRecognizer(hideViewGesture)
func hideView() {
containerView.isHidden = true
}

Its not merely possible to get button action working within a button as
1) Adding a button [Large button] on ContainerView will cause always to detect Large button action and will not allow you to detect button inside it
2) If seen in case of layers large button layer is on top So Logically always large button will first come in Action not inside View of containerView
Possible Solutions :
1) try to make use of gestures on ContainerView
2) you can use a segmented control as show and hide or a UIButton that is placed on side of containerView not over it So you will be able to perform all the required Actions

This is an old question but there actually is a simple solution, by overriding hitTest(_ point: CGPoint, with event: UIEvent?).
Inside your outer button:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.innerButton.frame.contains(point) {
return self.innerButton
}
else if self.bounds.contains(point) {
return self
}
else {
return super.hitTest(point, with: event)
}
}
It tests to see if the touch point is within the inner button (or you could have several inner buttons), if so it returns it and the touch is registered only by the inner button, if not it goes to the outer view. If neither view contains it, it calls super so the touch is handled correctly by other views.

Related

How to add Swipe Gesture on a view that has a Pan Gesture as well?

So I have a view with a carousel that has two views in it. I have two buttons that navigate from the two views inside the carousel. When the user is on the right view, the right button is disabled and left is enabled. When the user is on the left view, the left button is disabled and right is enabled.
However, I have a Pan gesture that allows the user to also swipe in between the two pages without using the buttons.
When I use the gesture to move in between the two views, I also need the buttons to reflect the change (either be enabled or disabled), however, when I put it on my pan gesture function it doesn't work 100% of the time as a half swipe or short swipe not strong enough to shift the pages but is strong enough to disable/enable my buttons as if the view has changed even though it hasn't.
I tried incorporating a Handle Swipe but I couldn't get the function to work.
Can anyone take a look at this code and let me know if anything is missing? Or better yet, if there is an easier solution to accomplish my goal
#objc func yesswiped (recognizer: UISwipeGestureRecognizer){
print("swipe pls")
}
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}

How can I have a button and scroll view in the same spot?

To build context, my app has a stack of cards (similar to Tinder) that flip and each could contain a lot of text on one side that require a UITextView for scrolling. It's basically a flashcard app, so once the user is done looking at the card, they swipe it away to view the next one.
I'm trying to make it so the user can tap anywhere on the card to flip it, so I added a clear button that fills up the card. My problem is that when the button is at the front, it blocks the text view from being scrollable, and when the button is at the back, the text view blocks it from being pressed.
Is there a way around this somehow to allow for both functionalities?
You can create a UITapGestureRecognizer, like so:
let gesture = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
addGestureRecognizer(gesture)
And the function to trigger:
#objc func tap(gesture: UITapGestureRecognizer) {
print("Tap!")
}
Maybe you can add a UITapGestureRecognizer to the text view itself. Something like:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(_:)))
textView.addGestureRecognizer(tapGesture)
#objc func didTap(_ gesture: UITapGestureRecognizer) {
// Handle the tap
}
did you try a tableView or a collectionView, you can customize your cell to look like a card then when a user clicks the cell it will flip or present a popup view, thats easier.

check if user clicked outside view controller

I am working with swift 4 for macOS and I would like to dismiss a view controller, if I clicked outside of this view controller.
With this code I can check, if the user has clicked into the view controller. but how can I check, if the user has clicked outside the view controller?
override func viewDidAppear() {
let gesture = NSClickGestureRecognizer(target: self, action: #selector(clicked))
gesture.buttonMask = 0x1 // left mouse
gesture.numberOfClicksRequired = 1
self.view.addGestureRecognizer(gesture)
}
#objc func clicked() {
print("Hello world")
}
NSEvent has a method called...
+ (id)addLocalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(NSEvent * _Nullable (^)(NSEvent *))block;
...that captures events before they are dispatched.
Ask to monitor mouse up or mouse down events and use the block to compare the coordinates to your view's bounds.
Edit:
Except, in Swift, it's called...
class func addLocalMonitorForEvents(matching mask: NSEventMask, handler block: #escaping (NSEvent) -> NSEvent?) -> Any?
I would use touchesEnded: then use the event to grab the locationInWindow. If the location falls outside your bounds of your view then dismiss it. A similar question for iOS can be seen here.
IOS - How to hide a view by touching anywhere outside of it
I always prefer touchedEnded because if a user accidentally clicks outside they can still drag to the view to cancel the dismissal.

Pop view controller using Screen edge pan gesture recogniser not following the thumb

As soon as I've added custom bar navigation bar button item I've lost the ability to use the default function to go back. I'd like to have the ability to use "swipe from edge" to go back.
I've added the Edge Pan Gesture Recogniser and connected it to #IBAction, but the dismissing action happens completely as soon as the pan gesture is recognised.
Instead of slowly following my thumb (as seen in other apps), the current view moves out with predefined animation.
How to make the animation following my thumb using Edge Pan Gesture Recogniser?
#IBAction func edgeSwipe(sender: AnyObject) {
navigationController?.popViewControllerAnimated(true)
}
There's no need to add Edge Pan Gesture Recogniser. Following #beyowulf's suggestions I've been able to implement the swipe to go back feature that behaves the same way as the default system implementation does - the views edge follows my thumb as I swipe it to dismiss it.
So I've removed the ScreenEdge Pan Gesture Recogniser from the storyboard and also removed the related #IBAction.
I've made my first view controller to be the delegate for interactivePopGestureRecognizer. Here's the code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.interactivePopGestureRecognizer?.delegate = self
}
}
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return navigationController?.viewControllers.count > 1 ? true : false
}
}

Using UIPageViewControllers scroll over entire view, including UIButtons

I have a UIView with a PageViewController and 4 UIButtons as subviews. The PageViewController and the button subviews are independent from each other. (PageViewController scrolls across 4 pages while the UIButtons remain constant)
The problem I'm facing is that the buttons take quite a bit of space and I'd like to use this scroll gesture anywhere on the UIView including the areas with buttons.
If the user taps on the button, its respective action would occur but I'd like if I can START a scroll/swipe gesture on a button and have the PageViewControllers scroll feature work.
I've tried/searched several ways including using UIScrollView instead of a PageViewController. Any help would be greatly appreciated. Thanks in advance. :)
Just add your own gesture recogniser and watch for gestures which velocity is greater in the x direction and going from left to right.
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if (gestureRecognizer.isMemberOfClass(UIPanGestureRecognizer)){
let point:CGPoint = (gestureRecognizer as UIPanGestureRecognizer).velocityInView(self)
if (abs(point.x) > abs(point.y)){
return true
}
}
}
Handle the gesture over here:
func handlePanGestureRecognizer(gesture: UIPanGestureRecognizer){
let translation:CGPoint = gesture.translationInView(self)
if(translation.x > 0){
//its going from left to right...
//...transition to the previous page
}else{
//...transition to the next page
}