Hiding a delete button when you tap outside the view - ios

I have a delete button that shows up when I swipe left on a table view's row.
I want to be able to hide this button if the user taps anywhere else in the view. How do I do that? I tried putting a giant button on bottom of all views but the tap outside is not being detected by the button.
In the viewDidLoad, I added the view controller as a target:
[self.backgroundButton addTarget:self action:#selector(backgroundButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
And in the callback I just have a message:
- (IBAction)backgroundButtonTapped:(id)sender {
NSLog(#"BACKGROUND VIEW TOUCHED");
}
But when I tap outside in a general area, I do not see the message.

I resolved the issue as follows:
. Created a view TopView subclassed from UIView
. Changed class of my top level view to this class.
. Within my logic at the proper place, stored the subview that needs to be hidden in the top view
self.view.mySubview = subview;
. Overrode hitTest:withEvent in top view. Here I detect the tap outside that of the subview and hide the subview as required.

Related

How to get index of multiple scrollview in iOS

i have horizontal and vertical scrollview, for example one scrollview have four button and and second scrollview have six button and more so, when i click on any button then got index of last scrollview view, i already using tag.
my problem is for example i click on third scroll then i want both tag
third scroll tag and specific button tag.
first tag of scrollview in storyboard .
set tag to button dynamically.(while create button or add button to scrollview)
track your index as below :
- (IBAction)clickonbutton:(UIButton *)sender {
UIScrollView* yourscrollview = (UIScrollView*)[sender superview]; // track your scrollview as per your view hierarchy
NSLog(#"%ld",(long)yourscrollview.tag);
NSLog(#"clicked button tag%#",sender.tag);
}

Prevent a swipe gesture while another UIViewController is present

I have an app that has a mode that users enter into when editing a geographic feature. While in this editing mode, I present a view controller with an embedded tableview that is inset from the view controller's frame. The background of this view controller has an alpha of 0.5 so the underlying view is still partially visible outside of the inner table view, though grayed out by the semi-opaque view controller on top. There is a button on the underlying view controller that is used to slide the view to the right when not in editing mode.
While the editing view controller is active, this button to activate the slider is disabled, which is desired behavior. However, users can also swipe the view to the right which achieves the same things a the button. i would like to disable this swipe feature while the editing view controller is present.
So I guess my question is: how can I disable a swipe gesture when a certain view controller is present?
You can use the gestureRecognizerShouldBegin: delegate method. If the detected gesture is that particular swipe gesture (i.e. theGesture) and that certain view controller is present (i.e. theView), tell the gesture not to begin, ex:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == theGesture && theView.window != nil) {
return NO;
} else {
return YES;
}
}

Create a draggable UIView on long press of a different view

I have implemented something that does the following:
I am showing a full-screen UICollectionView with different items. Underneath this view (out of sight) is a "detail" view for one of the items.
When a user long-presses one of the UICollectionView items, I would like to hide the UICollectionView to reveal the "detail" view behind.
I will then instantiate a new UIView which is hovering over this view and is draggable. The user can then drag and drop this new UIView into position on the "detail" screen.
The only part which I am having trouble with is the syncing of the long-press with the draggable view. I would like the user to long-press on the UICollectionView, and then be immediately dragging the draggable view.
How do I connect these two actions?
At the moment the UICollectionView calls a delegate on the lower view, which instantiates the draggable view:
- (void)contactAlgorithm:(ContactAlgorithmViewController *)contactFlowViewController didLongPressContact:(CDContact *)contact {
DraggableContactView *draggableView= [[DraggableContactView alloc] initWithContact:contact];
[self.view addSubview:draggableView.view];
}
But how do I make this draggable view the view which is being dragged by the long.press?
First get the frame of the item which was long pressed.
Add the draggable view at exactly that frame.
Now, add pan gesture recogniser to that view and set delegate of that gesture to viewController , and whenever move occurs delegate method will be called.
There change the frame of draggable view to follow touch.
Instead of adding pan gesture, you can use touchesMoved method of viewController.
CODE:
using below code, you can drag the view without using pan gesture.
//have a class property of DraggableContactView
#implementation ViewController{
DraggableContactView *dragableView;
}
Selector method of longPressGestureRecogniser
-(void)longPress:(UILongPressGestureRecognizer *)ges{
if(dragableView==nil){
dragableView=[[DraggableContactView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
dragableView.backgroundColor =[UIColor greenColor];
[self.view addSubview:dragableView];
}
CGPoint p =[ges locationInView:self.view];
[dragableView setCenter:p];
}

Capture Event of Subview Class in View Controller

I have two subviews with a UIbutton on them what I want is I want to capture the button click in my View Controller, and change the color of a view residing in it, one way is to create an object of View Controller in subview class and directly change the color of view but I dont want to do that can anybody tell me the other way around.
In your view controller's viewDidLoad, add this:
[subview.button addTarget:self action:(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
where subview.button references the button inside the relevant subview. Also, add this event handler to the view controller:
- (void)buttonWasTapped:(id)sender {
// Button in subview was tapped; change the colour of the view here
}
and implement the logic you need to execute when the button is tapped.

Overlap iOS components viewController

I have a UITableView in my View Controller and I would like to place two buttons (one on the left and other on the right side of the UITableView), overlapping this component.
How would I do this? If I place a button in one of the sides, the UITableView shrinks to fit the new button, but I'd like to view it over the table.
Create the button as a subview of the view controller's view (this is easiest to do in code, because interface builder would automatically add the button as a subview of the UITableView
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(40,40,80,80)]; //Whatever your need and repeat for your second button
[self.view addSubview:button];

Resources