Swift UIScrollview double tap uitbutton zoom - ios

I have a view hierarchy like this:
UIView -> UIScrollView -> UIView (contentView) -> UIButton
I have added code to the UIScrollView to activate zoom when the user double tap.
This is working well, the content view and the UIButton are zooming.
In my content view, I have some UIButton, my problem is when I double click on this button, I want the scroll view to zoom (this is ok) but I don't want my button to fire the event (this is not ok)
I want the button to only fire an event if the user click on it one time, but not with double tap.
In my case, the UIButton can be small, and I want the user to get the possibility to double tap on it to zoom, but to fire an action only if it is a one click.
I try to do this with swift
thanks a lot for your help

Instead of UIButton, use UIView or UIImageView as button and add UITapGestureRecognizer to it.
And implement the following logic:
if the view is double tapped {
call your zoom function
}
if the view receives single tap {
then perform your button click function
}
Hope it helps :)

Related

Calling endEditing on a UIView

I have a tableView with two UITextViews. The user can add as many cells as they want by tapping on a "+" button. The tableView starts out with one row (cell). Lets say the user adds some information to the two textViews and then taps the plus button and scrolls to get to it. I call this function to dismiss the keyboard when a user scrolls so it isn't in the way:
override func scrollViewDidScroll(scrollView: UIScrollView) {
self.view.endEditing(true)
}
Now, the keyboard disappears and the user goes to tap on the second row's text field. The tableView jumps up (I think to accommodate the keyboard), but the textField can't be edited (no cursor and keyboard doesn't appear). If I go back up to the first row (row 0), the keyboard appears and I can edit the textView. Why is this happening?
There is a property on scrollView subclasses (e.g TableViews)
tableView.keyboardDismissMode = .OnDrag
Once the user begins a descending drag it dismisses the keyboard
scrollViewDidScroll calls everytime the tableview moves, so when "The tableView jumps up (I think to accommodate the keyboard)" it also calls and close the keyboard. You should add there some confines or put endEditing code in the other place.

Xcode iOS UIView with tap gesture allowing input on subviews

So I have a homepage view that people see when they first boot up the app, it has several different options. Yesterday I added a tutorial overlay, so the first time they open the app I have a view that is 0.5 alpha black and is fullscreen over the other view, this view has white text and arrows pointing to functions on the page. I attached a tap gesture recognizer to this view and its action is to call the next tutorial screen ( 3 in total). Afterwords they all are hidden and the app acts as normal. This all works, except even though my view is full screen, with User Interaction Enabled checked and a gesture tap recognizer, the gestures / touches on the sub view still work. So if I just tap some white space on the app the tutorial and tap gesture work as expected, cycle me through the pages etc and works. However if I accidentally press a sub view button or do a gesture from the subview it will do the action (take you to a new page or w/e)
So basically as far as I can tell my view despite being on top of the other and having a tap gesture recognizer attached to the view the sub views are still getting pressed, any ideas?
Check the tap event view before doing any operation..
- (IBAction)yourTapEvent:(UITapGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
if(piece == (view on which you have added gesture))
{
//Do your necessary operation
}
}
try this it would help you to disable gesture on its subviews
- (void)tapAction:(UITapGestureRecognizer *) gestureRecognizer{
UIView *v = [gestureRecognizer view];// Guesture added view
NSArray *temp = [self.view subviews];
for (int i = 0; i<temp.count; i++) {
if([temp objectAtIndex:i]== v){
// Do your swipe tap action here
}
}
}

Allow taps on YTPlayerView to play video or have button overlay the YTPlayerView

I'm using YouTube iOS Player Helper View and the player itself works wonderfully. However, I want the user to be able to tap on the video and have it start playing. I tried adding a UITagGestureRecognizer to the YTPlayerView, but it didn't register any of my taps.
I added a UIButton to my view and connected it to my IBAction that tells the YTPlayerView to play and that worked fine. However, I want it to appear to the user as if they've tapped the video itself. So, I resized my UIButton to be the same size as the YTPlayerView and put the UIButton on top of the YTPlayerView.
Taps on the UIButton no longer work.... almost. Any taps to the left, right, or bottom of the YTPlayerView don't work. Taps above the YTPlayerView still go through. So, if I shift my UIButton 10 pixels up, I can tap just on the 10 pixel strip and my IBAction will get called. If I move the UIButton 10 (or 30) pixels to the left, right, or bottom, none of the taps make it through to my IBAction.
In the image below, my UIButton has a green background with transparency so that you can see what's behind it. (I have tested this with it fully opaque and it behaves the same way, the transparency is not the issue.) If I tap in the green area inside the red box (i.e. above the YTPlayerView), then my IBAction is called as expected. If I tap inside the green area outside of the red box (i.e. left, right, or below the YTPlayerView), then I never received my IBAction.
This whole view is enclosed in a UIScrollView. I tried adding a UITapGestureRecognizer to self.view and when I try tapping the YTPlayerView, that tap is delivered to me as if it hits the UIScrollView instead of the YTPlayerView.
Ignore touches to scrollview or forward touches from scroll view to the button
Add gesture to the playerview
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (someScrollView.superview != nil) {
if ([touch.view isKindOfClass:[UIButton class]])
{
return YES; // handle the touch
}
}
return NO; // ignore the touch
}

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];
}

ios - button not responding to click after I change its coordinates

I have a button that I configure like this in my .h file:
- (IBAction)addBusiness:(id)sender;
#property (weak, nonatomic) IBOutlet UIButton *planBusinessButton;
then I do this in my .m file:
- (IBAction)addBusiness:(id)sender
{
NSLog(#"Plan business clicked 1");
}
- (IBAction)planBusinessButton:(id)sender
{
NSLog(#"Plan business clicked 1");
}
but in my onLoad I do something like this:
CGPoint pt = planBusinessButton.center;
pt.y -= 275;
planBusinessButton.center = pt;
The problem is that the button click is not firing in the IBAction when I click the button.
Would the changing of the location of the button have an effect on why the button is not getting clicked?
Why would the button click not fire in the IBAction?
Thanks!
EDIT: Moving the button should not affect it's action firing, unless it's moved off the view or being below some other view that would intercept the tap.
If tap is not firing the button, check that:
IB connections are properly connected
methods exist in the proper location
button is enabled for user interaction
Try to avoid doing any view modifications in viewDidLoad method, as the view is not ready yet...
instead, try setting the button center in viewDidAppear method.
Possibly. Gesture Recognizers could be handling the events in the location the button moved to or there could be another view blocking the button at the new location and you need to bring the button to the front. Also, you should put this into viewDidAppear or an IBAction if you want an event to trigger it.
make sure that your planBusinessButton.frame is not outside its super.frame .
eg:if the button`s frame like this (0,-200,100,200), when the button added to its super view it will not handle the click event.

Resources