I have a UIScrollView filled with buttons that I created programatically.
Paging is enabled and whenever users scroll through the pages rapidly they always end up accidentally clicking buttons that they didn't mean to... Should I be using TouchUpInside or is this the reason for my problem?
One thing I've noticed is that if a user presses the button then slides the scrollview doesn't move but if they slide the scroll view very quickly and let go half way through the scroll and they grabbed the scrollview on the button then the button will become "pressed" is there any way I can have the buttons not do anything if they are clicked while scrolling is in progress or is the solution simpler than that (i.e. just changing the touch event to a different listener)
This did the trick!
setExclusiveTouch:YES
Related
I am new to swift and iOS development. I have created a scroll view with table view cells and image views inside cells. Each Image view has a button with same constraints. I have a fixed button in the same screen (irrespective of scroll view) and I am not sure how to achieve the click for it since every time I click on it, the background button is being clicked. I saw some solutions about disabling and enabling the buttons but in this case, I will not be sure which button will be in the background since it is a scroll view. Any help on how to solve this will be appreciated
Since I don't see your code, I can't tell you the exact fix but I assume that the question is how to determine the button's position in the parent view hierarchy. Every UIView has a method called bringSubviewToFront(view:) so if this was the case, then, you can use this method to bring your button to the front.
I have created a scrollview, to scroll through an array of images, and I have a button and soon to be 6 more on the screen. Now to make the photo nicer, I would simply like to make the buttons disappear and when tapped on again to appear.
Now I know how to do this with just a regular view with simple isHidden, and how to make it reappear when tapped on, but I am largely confused on how to do it on a scrollview because on a regular view, I just put a button over the whole screen but with the scrollview I can't. Any ideas anyone? Still getting better at this one step at a time, and one question at a time.
Here is a screenshot: http://imgur.com/a/Ll6QO
Use a gesture recognizer on the scrollview.
Add a touch gesture recognizer to toggle the visibility of your buttons. Be sure that the gesture recognizer doesn't cancel touches in view, otherwise you won't be able to scroll.
I want to have a scrollview that has 2 views on it. Both views fill up the screen, and when you push a button on one view it uses scrollRectToVisible to move to the other view.
I want the user to not be able to scroll it with their touch though, so disabling scrolling doesn't work. Obviously i could have scroll become re-enabled when the user touches the button than lock it again afterwards, but then the user could grab or interact while it's unlocked in the scroll animation.
Any ideas?
Woops. As it turns out, against some/lots of logic, you can actually disable scroll on the scrollview, but scrollRectToVisible still works, it'll only stop it working if you disable user interaction - i'me guessing because the view with the button inherits from the scollview, so the button wasn't actually working at all.
I have a Scroll View with a View (content view) inside of it. I've added two buttons to test. The Scroll View scrolls fine but the buttons within the content view are not clickable. I've seen plenty of posts saying this issue happens when they programmatically add UIButtons to the View, but I am not doing this. Here is my exact process:
Drag the Scroll View onto the main view. Add 4 constraints
Drag the Content View onto the Scroll View. Add 4 constraints.
Add 2 Buttons (one high and one low to test scrolling) to the Content View.
This is all I am doing, no code at this point. Is there anything else I have to do to allow the buttons to be clicked? Here is a screenshot of my xib:
Update:
When hooking the button up to a simple IBAction and logging a message, it turns out it IS being pushed and working properly. However, when the button is pushed, the button isn't changing colors like it should (its "pressed" state is not appearing). Any ideas why?
First make the button to custom type
Select button from storyboard then on right attributed inspector change its "state config" to whatever you need like Highlighted, selected, Disabled and default and choose the colour for each state.
Now you can see the colour change on that button.
A UIScrollView first needs to know whether or not you are going to scroll before letting the touch pass to its child views. If you want the child views to get the touch immediately, set the UIScrollView's delaysContentTouches to NO.
If you just put a UIButton in a UIScrollView, the button does not highlight when you tap on it quickly. Instead, you have to tap and hold for a while before the button highlights. According to the documentation, this is because UIScrollView waits a little to determine if the user intends to drag the scroll view before forwarding touches to the views in the scroll view.
As noted in several Stackoverflow answers (such as this one: https://stackoverflow.com/a/19656611/2135004), the solution is to prevent this delay by setting the delaysContentTouches property of UIScrollView to NO. (An additional step is to subclass UIScrollView to override touchesShouldCancelInContentView: to return YES for UIButtons, so the user can still scroll the scroll view if the user initially touched the button in the scroll view.)
However, interacting with the App Store app seems to suggest another solution. (Although it may be a little picky, one reason I am not going with the solution where delaysContentTouches is set to NO is because I do not want the button to highlight if the user quickly drags on a button to scroll the scroll view. This is actually the highlighting behavior UITableViewCells.) Consider the following screenshot of the App Store app:
The blue "Quick Links" buttons (namely, "App Collections," "Game Collections," and "Apps Made by Apple") seem to be implemented as UIButtons with the UIButtonTypeSystem style because their highlighting fades in and out as you drag your finger in and out of them. In addition, the scroll view's delaysContentTouches property does not seem to be set to NO because the blue "Quick Links" buttons do not highlight when you quickly tap on them. However, the gray bordered buttons (namely, "Redeem," "Send Gift," and "Apple ID: ...") do highlight when you quickly tap on them! (Their highlighting also fades in and out as you drag your finger in and out of them.) Also note that the buttons do not highlight when you just happen to be touching them when you drag the scroll view.
How are these gray bordered buttons implemented? The key behavior I want is that they highlight when you quickly tap on them in a scroll view that delays content touches. (They should also highlight and unhighlight as you drag your finger in and out of them like plain UIButtons, preferably by fading their highlighting.) I do not see how this could be easily achieved by just subclassing UIButton, and I'm not even sure if somehow subclassing UIControl to create a new button class would work either.
I think what's happening here is that the grey button's highlighted state is being set when its action fires, then unset when the resulting alert or sheet is shown.
(If you're not showing an alert or a sheet, you'll just have to arrange for it to be unhighlighted on the next run loop.)