change font in UITapGestureRecognizer - ios

I have a UIView, in which I have one UILabel. I would like to change the UILabel.Font to a custom UIFont when the UIView is tapped (This enlarges the tappable area instead of using a UIButton).
To add the UITapGestureRecognizer, I use the following code:
var gestureRecognizer = new UITapGestureRecognizer ((item) => {
(item.View.Subviews[0] as UILabel).Font = Constants_iOS.FONT_OS_SMALL_LABEL_SEMIBOLD;
});
view.AddGestureRecognizer (gestureRecognizer);
The font change gets executed, and stepping through the code shows the font changing. But it does not update in the UI, and all subsequent executions of the function also start with the Font being the old one.
What is going wrong?

Add tap selector method.
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
view.addGestureRecognizer(tap)
func handleTap(sender:UIGestureRecognizer){
//change your font here
}

Please note that a UITableview header gets redrawn each time it gets tapped. So your font change will not persist.

Related

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.

Adding gestures to a draggable view? Swift

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.

UITableViewCell tap on subview without cell selection

I have a custom tableviewCell with an UIImageView imgView inside. I added UITapGestureRecognizer to imgView, so that I can change image of imgView whenever I tap to it. However, when I tap to imgView, didSelectRowAtIndexPath is also triggered. What I want is:
When tapping outside imgView: Trigger didSelectRowAtIndexPath
When tapping inside imgView: Block didSelectRowAtIndexPath, just call gesture callback to change image of imgView.
Callback function is already called when I tap inside imgView but I can not block didSelectRowAtIndexPath in that case. I've been searching around for hours but have not found the solution yet. I'm using objective-c. Anyone have any idea for my problem? Thank all!
Update
Finally I found out what my mistake is.
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onFavouriteIconTapped:)];
tapGesture.cancelsTouchesInView = NO; // this should be YES by default
[_ivImageView addGestureRecognizer:tapGesture];
Just set cancelsTouchInView = YES fix my problem. If the value is YES, touch event will be consumed by ivImageView and cell wont be selected. Hope this will help someone who has the same problem.
Your approach is fine. Your tap gesture selector is not getting called probably because you might have not enabled user interaction of the UIImageView.
imageView.userInteractionEnabled = TRUE;
Default value of userInteractionEnabled is NO, for UIImageView.
Once your selector starts getting called, you will get the desired behaviour i.e on click of imgView, tap gesture selector gets called and on touch outside the imgView didSelectRowAtIndexPath will get called. This is in accordance to iOS responder chain rules.
#SHN ,you are saying right. May be #meaholik , you have missed that line.
imageView.userInteractionEnabled = TRUE;
You can also check this
link.
Hope it will help you. :)

UITapGesture removing ability to select from UITableView when resigning keyboard responder

So I'm having an issue where I have added a UITapGestureRecognizer to resign first responder status of associated textfields so the keyboard will drop away.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddMedViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
func dismissKeyboard() {
medNameText.resignFirstResponder()
doseAmountText.resignFirstResponder()
}
This works fine for getting rid of the keyboard but not when I want to make a selection from a UITableView that is embedded in the same view. (The text fields are located outside this table view).
Essentially each table view cell represents a pre created specific time that is chosen to match the text entered into the text fields then passed along all together to another view.
Any Idea how I can get the UITableView to still recognize the selection of cell when tapped while resigning first responder of the text fields?
I have tried adding a gestureRecognizer function but as I am new to Swift(and an amateur coder) I am not exaclty sure how it works. Any help would be greatly appreciated!
Thanks.
try to add tap.cancelsTouchesInView = false
It means touches are delivered to a view when a gesture is recognized.

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.

Resources