Adding gestures to a draggable view? Swift - ios

I've got buttons on my view that are draggable using touches began/moved/ended.
I want to add a tapped and doubletapped actions for my buttons. Once I switch my button's class to UIButton the action I've created works, but once I change it back to DraggableView the actions stop being called because I guess touchesBegan overrides any other touches on the view.
Is there a good way to do this?

First of all you have to implement UITapGestureRecogizer delegate in your class and add following line of code.
let tap = UITapGestureRecognizer(target: self, action: "handleTap:")
tap.delegate = self
tap.numberOfTapsRequired = 1
yourButton.addGestureRecognizer(tap)
Hope this helps.

Related

Recognize both UILongPressGestureRecognizer and UIPanGestureRecognizer on same UIButton

I want to make a UIButton that when you long press it, it will start recording video and if you pan your finger vertically up (while still long pressing), the video will zoom in.
To my button I added a UILongPressGestureRecognizer and a UIPanGestureRecognizer that does just that. Individually, they work. However, they do not work together.
How can I make my button record when long pressing but also allow me to pan my finger and have that recognized as well? This is how I added my recognizers:
let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
button.addGestureRecognizer(long)
let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
button.addGestureRecognizer(pan)
You need to confirm the delegate of those two gestures.
for ex:
let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
long.delegate = self
button.addGestureRecognizer(long)
let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
pan.delegate = self
button.addGestureRecognizer(pan)
and there is a delegate method to recognize multiple gestures simultaneously.
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
define that in your class and return true.
you will get what you want.
I know this wasn't exactly what the question was asking but you can actually bypass having to use gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) and use UILongPressGestureRecognizer as a UIPanGestureRecognizer using the UIGestureRecognizer.State changes. Thats what i've done in the past, cleans things up and makes more logical sense than having two gesture recognizers

iOS11 UIBarButtonItem action not get called

I used Xcode9 Beta6 to build the project, the action was called correctly on iOS10 device, however it is not work on iOS11 device.
In My project, there are some viewControllers have a UIToolBar on the top, and the toolBar contains some UIBarButtonItems.
There is one this kind of viewController, whose UIBarButtonItem action is not called when I tap the UIBarButtonItem. I can see the tapping animation (the icon become dim first and back to normal after finger released)
At the end of viewDidLoad, I print the info of toolbar.items to indicate that target action are set properly.
Debug Output
In my case I was setting up the button and instantiating it as a property of the vc
class myVC: UIViewController {
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}
If I moved this to ViewDidLoad it resolved the problem
class myVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}
}
I solved this problem by removing a current gesture recognizer from my view and adding a new one. Than I opened the connections inspector of my view and add gestureRecognizer connection to my gesture recognizer.
Apple has confirmed this bug.
My temporary solution is changing the gesture recognizer area by removing the overlap area, so that the tap gesture won't block the tap event on UIBarButtonItem.
It is happening only for iOS 11 and when custom UIView used for rightBarButtonItem (or left also). If you are using UIBarButtonItem then it will work fine.
There is 0 width of this custom bar item, so we need to set it to something.
In viewDidLoad of this controller you can add code, you can replace 100 to something what will work for you:
if let view = self.navigationItem.rightBarButtonItem?.customView {
view.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
At least as an easy temporary solution it is fine.
In my case the problem was a gestureRecognizer added to the whole main view (in order to close the keyboard) like this:
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeKeyboard)];
[self.view addGestureRecognizer:gesture];
That gesture recognizer overrides the tap on the UIBarButtonItem, thus I solved by creating a new subview placed immediately below the navigation bar and assigning the gesture recognizer to that view and not to the whole main view.
Mark the method you are targeting at with #objc.

Single tap gesture to multiple View using storyboard

How to have single tap gesture to multiple Views using storyboard.
I drag 3 views and one tap gesture into UIView class.
Contacted three view to tap gesture and added action class handleGesture Method on tap on any one the tree view it should trigger the method action.
using story board.
But i want to do it with single tap gesture is it possible or not.
try with this, define a variable UITapGestureRecognizer with your method, after that, in a method or wherever you want, you can add this gesture to your multiple views
var recognizerMovements: UITapGestureRecognizer {
get { UITapGestureRecognizer(target: self, action: #selector(self.myActionMethod)) }
}
self.myFirstView.addGestureRecognizer(myActionMethod)
self.mySecondView.addGestureRecognizer(myActionMethod)

selector is not called 2 tapgesture

I have added a UITapGestureRecognizer to a view, but when I click it the method is not being called.
func addTapGestuere(uiview: UIView) {
let tapGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("cardTapped:"))
uiview.addGestureRecognizer(tapGestureRecognizer)
}
I run this on the viewDidload
self.addTapGestuere(self.Card1View)
self.addTapGestuere(self.Card2View)
I put a break point on the method
cardTapped(recognizer: UITapGestureRecognizer) {
}
but when I click on the image the method isnt called. I have user interaction enabled for all the views.
Be careful about the following:
The UIView you are trying to add the UITapGestureRecognizer has userInteractionEnabled set to true:
self.view.userInteractionEnabled = true
The UIView you are trying to get the tap to has no other views covering it. Use the View Debugger to confirm this.
Most importantly, make sure you are adding the UITapGestureRecognizer to the correct UIView. Adding it to self.view will add it to the UIViewController's view.
As a side note: You can add the UITapGestureRecognizer using the Inetrface Builder itself, then connecting IBAction for the same. Will reduce the probability of simple mistakes.

How can I add multiple taps to a button or a UIView?

I want one of my screens to be accessed only if the user taps three times on a button within a second. eg. if there's a button on View controller A, a user should tap 3 times within a second on that button to get to View Controller B.
Any help is appreciated!
Buttons are not set up to respond to multiple taps. You'll have to simulate that yourself.
As others have said, you can create a tap gesture recognizer and attach it to any view. For some views you'll need to set the userInteractionEnabled flag to true before it will respond though.
If you want a button to handle double-taps you'll need to have the button with no action installed but a 2 click gesture recognizer attached.
You need to add tap gesture recogniser to your custom View.
-(void)handleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer{
NSLog(#"3 tapped");
//add code to present another View Controller
}
-(void)buttonView{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGestureRecognizer.numberOfTapsRequired=3;
[self.customButtonView addGestureRecognizer:tapGestureRecognizer];
}
Swift:
func handleTapGesture(tapGestureRecognizer: UITapGestureRecognizer) {
NSLog("3 tapped")
//add code to present another View Controller
}
func buttonView() {
var tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
tapGestureRecognizer.numberOfTapsRequired = 3
self.customButtonView.addGestureRecognizer(tapGestureRecognizer)
}
You can call this method in viewDidAppear.

Resources