iOS: Making only one UIView to respond - ios

I have several UIViews (CollectionView, TextField, etc) in my ViewController. If the user presses any item in the collectionview, a new collectionview (smaller) is to be shown from the bottom.When the new collectionView is being shown, I want only this view to respond to touch events. If the user taps outside this view, it should be taken off the viewController. The functionality is similar to PopOverController. I am using
[subCollectionView becomeFirstResponder]
but the other views are also responding to touch events.
I wish I could put the question more clearly, Let me know if it is not clear.
Thx!

No need of setting firstResponder of a UIView.
All you need to do is
create a view named bottomAnimateView(which will come from bottm) of size =
self.view.frame and background color as clear color.
add a UICollectionView over it.
3.Add UITapgesture over bottomAnimateView and do the stuff of removing or hidding it from superView.
Refer the attachment .Which might help you.

Related

Custom Keyboard Accessory view input

I have created a custom accessory view to supplement the standard Apple alpha iOS keyboard.
The purpose is to add a line of numeric keys to prevent flipping back and forth between keyboard views. At first, I created a toolbar and loaded it with a set of 0 - 9 titled buttonItems and it functioned quite well. However, it looked terrible, not at all like the alpha keys despite adding a rounded rect background image to each key because the system apparently prevents customizing font size and button spacing inside the stack view of the toolbar. Therefore, I created a UIView xib and loaded it with a stackView full of customized numerical buttons. When I add the UIView as the accessory view it looks pretty darn close to the rest of the Apple Alpha keyboard. The issue now is that the touch-up events go to the UIView class of the accessory view. Is there a clever, efficient way to have the button presses in the accessory emulate the std keyboard feeding into TextField: shouldChangeCharactersIn? I could package the button presses into a local notification event to get it into the class holding the textField but that seems terribly inelegant! Any suggestions would be greatly appreciated. Stay Safe!
Not the best answer, but I did implement notification on key button press with an observer in the main view class. The observer does a TextField.insertText which is suboptimal since I will need to refactor the several hundred lines of code that performs real-time language translation in the shouldChangeCharacters methods. Ah well.

UIButton default tap animation inside UITableViewCell

I have a few UIButtons inside UITableViewCells, and all of them are missing default tap animation, however if I long press - animation works. I've found some solutions, like setting type to .system, showsTouchOnHighlight = true, but none of them have helped. What is the problem here?
It's not a "problem" - it's by design.
When you have an object such as a button in a table view or scroll view or collection view, and you "touch" it, the system needs to know whether you are tapping the button or if you want to touch-and-drag to scroll the view that contains the button.
So, the table view (really, the "scroll view part" of the table view), waits to see if you drag your finger or not before it performs any actions.
If you want the button to respond immediately to a touch, you need to set
delaysContentTouches = false
on the containing scroll view(s).
If you search for uibutton inside uitableviewcell delaysContentTouches you should find plenty of discussion on this topic, and various approaches to change the default behavior.
For this problem you can add extension to UIButton and override some methods.
You can check my answer here

Button Responsiveness at UIScrollView

I am dynamically creating a uiscrollview and i place some uiview's that contains some label and buttons inside to display some news. Every button inside the sub uiview calls a rest function to like, unlike or share the news. Some of the buttons opens overlay screens like comment news. I am assigning actions to buttons inside the main form that contains the uiscrollview.
When i click a button that opens an overlay screen. When i close the overlay screen and hit Like button, it does not respond to touches. After attempting one or two more times, it works.
Does anyone has any idea about this issue?
Are you using UITapGestureRecogniser for "Click action"? If yes, you propably want to set flag "cancelsTouchesInView = NO" for this recogniser.
Check to see if there is a clear view covering your button. That view will consume your tap, so the button never sees it.

UICollectionView didSelectItemAtIndexPath not called when tapped on UITextView

I have a UICollectionView with custom cells- They have a UITextView that mostly covers the entire cell. This presents a problem when using didSelectItemAtIndexPath. The only way to trigger it is by tapping outside the UITextView. I want it to trigger wherever in the cell you tap, whether there is a text view or not. How can this be done?
didSelectItemAtIndexPath is called when none of the subView of collectionViewCell respond to that touch. As the textView respond to those touches, so it won't forward those touches to its superView, so collectionView won't get it.
override hitTest:withEvent method of your collectionViewCell or CollectionView subclass and always return self from them.so it explicitly makes collectionView as first responder.
I would suggest to use UIGestureRecognizer for each cell and when it taped to send it to UITextView or whatever , perhaps there maybe a better solutions , but I would use this 1 because of simplicity reasons.
Do you override touchesEnded: withEvent: ?
I had the same problem today and I found that I have some customised logic in touchesEnded in one of collectionview's container views, and I didn't call
[super touchesEnded: withEvent:]
when I'm done with my customised logic in touchesEnded.
After adding the super call, everything is fine.
Select UITextView, in that specific case UICollectionViewCell, and switch to attribute inspector. The uncheck User interaction enabled and it should work fine.
I ran into this problem when I had a scroll view taking up my entire collection view cell. While all the solutions above probably work fine, I came up with my own elegant work-around. I put a 'select' label under my scroll view. Since the label is not part of the scroll view, it passes the tap event on to the collection view. It also serves as a nice indicator that an action is required of the user.
Just do this
textview.isUserInteractionEnabled = false

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources