Automatically scroll to a subview in a scrollview - ios

I have implemented a scrollview that has dynamic content: multiple sub-views displaying photos of players with player names under. The flow is:
1. On the main window, a player is selected.
2. User launches another view to add a new player.
3. When the user goes back to the main menu, the scroll view is refreshed completely so that the new player photo and name is also displayed.
At this time, the scroll-view should still have the previously selected player in focus but instead it is showing it from the beginning.
So the question is: is there a method to automatically scroll to a specific position in the scrollview so the screen still has the selected player in focus?

Yes, there are two:
(1) scrollRectToVisible:animated:
(2) setContentOffset:animated:
You can read about both methods at UIScrollView Class Reference. Either way, you can use an instance variable to store the content offset or the visible rect when the user scrolls, and then use one of the methods I listed above to restore the scroll view position when the player data is updated.
Depending on what you are doing, you may find UITableView easier to implement because it is very customizable "out-of-the-box" and it has plenty of methods to help manage a list (including the ones I mentioned above, and more). If you're interested, have a look at UITableView Class Reference.

Simple, create a CGPoint and a BOOL that you can retain after the view change and set them when you initially navigate away from that page. Then in viewDidAppear you can check if the BOOL is YES and use:
[myScrollView setContentOffset:myCGPoint animated:YES];
Or, if you want the scroll view to appear already scrolled to the correct position, do the same thing except in viewWillAppear with the animated flag set to NO.

Related

Putting Text into horizontally UIScrollView and appropriate AutoLayout - Swift

So I want to create a Slider on the top of the UI, each item representing a Category (there will be five items). I don't want to switch to another Controller, when sliding to another Category, I just want to load new data into the current controller. So I guess the UIScrollView is the way to go.
See here for what I want to realize:
I have trouble now to show the name of the chosen Category in the middle of the Slider, and by the same time on the left and right its neighbors.
Using a effective AutoLayout is a also necessary.
Is putting Panels into the UIScrollView the right way?
I am new to iOS-Development and would appreciate any help.
Add a scrollview to the top of your controller, in code configure scrollcontentwidth to screenWidth*5 and on each swipe change reload the data of your controller.

Implement iOS home screen folder

I have a collectionView with each cell showing an image. I want to implement a drag and drop. This will solve two purpose.
Rearrange cells.
When a image is dragged into other, it forms a collection (think of it as a group containing two or more images).
This behavior is inline with the home screen folder where app icon can be grouped into folders.
Please suggest how I can implement this.
This isn't trivial but it is not impossible either.
Reordering is simpler, and you can take advantage of UICollectionView's methods:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/doc/uid/TP40012177-CH1-SW67
Reordering Items Interactively
Collection views allow you to move
items around based on user interactions. Normally, the order of items
in a collection view is defined by your data source. If you support
the ability for users to reorder items, you can configure a gesture
recognizer to track the user’s interactions with a collection view
item and update that item’s position.
To begin the interactive repositioning of an item, call the
beginInteractiveMovementForItemAtIndexPath: method of the collection
view. While your gesture recognizer is tracking touch events, call the
updateInteractiveMovementTargetPosition: method to report changes in
the touch location. When you are done tracking the gesture, call the
endInteractiveMovement or cancelInteractiveMovement method to conclude
the interactions and update the collection view.
During user interactions, the collection view invalidates its layout
dynamically to reflect the current position of the item. If you do
nothing, the default layout behavior repositions the items for you,
but you can customize the layout animations if you want. When
interactions finish, updates its data source object with the new
location of the item.
The UICollectionViewController class provides a default gesture
recognizer that you can use to rearrange items in its managed
collection view. To install this gesture recognizer, set the
installsStandardGestureForInteractiveMovement property of the
collection view controller to YES.
To implement the "grouping" behavior, you need to define your cell in such a way that it can determine whether it is a single item and should just display itself, or if it consists of multiple items, in which case it should be able to pop up a view to act as a tray, possibly its own view controller to handle taps independently. This part is going to be hard as you'll need to implement reordering again, and dragging out, etc.
You might want to search Github and others to see if anyone has implemented a "Springboard" type of app that can get you started.

Completely prevent interaction with UICollectionView

I have a UICollectionView that I want to show, but I may place a UIView overtop of it with a slightly translucent background. When that occurs I need to prevent the user from interacting with the collection view - they should only be able to interact with the view that appears overtop of it. To do that, I've made the view fill the collection view's bounds and that works well. However I noticed when users of VoiceOver use the app, if they tap on the view it will focus it but then if they swipe right to go to the next element it will focus the first cell in the collection view and allow interacting with it. How can I completely prevent interacting with the collection view for all users?
I've tried setting scrollingEnabled to false and also userInteractionEnabled to false for the collectionView but that didn't do the trick. The label I have within each cell is still accessible, therefore the entire collection view is accessible.
Looks like accessibilityElementsHidden is the property you want; should be able to set this to YES on the UICollectionView to hide that subtree. From docs:
You might use this property to hide views that are covered by the arrival of a new view. In this case, the hidden views might remain visible onscreen, but they are not the focus of the user’s actions.
...which sounds like a good match for your case.
If users really should be only able to interact with the view that is on top of it and no other view (including the collection view), consider setting accessibilityViewIsModal on the view that is on top.
To nicely see what accessibilityViewIsModal does in practice, consider seeing the excellent interactive Figure 1 at Adding accessible behavior by David Rönnqvist, in section "Implementing accessible modal views".

UICollection view update callback

I have been googling about this but is there an easy way to catch the event when data in collection has been added or removed? I have collection view containing list of images in one line horizontally and user can scroll right or left to see the images. I want to bring the scroll position to the first image from the right when another image is added. I can do that by using scrollRectToVisible but I am not sure where I can call this function to scroll. I was trying to find an event fired when the collection view is modified.
And how exactly are you adding the image? Are you adding a new cell? If yes then you must be updating the layout parameters and invalidating the layout, you can add your scroll there. And if you are just adding an image on the already existing cell, then on what gesture are you doing it? Gesture Handler is a good place in that case.
This is bad architecture. Instead of the relevant parts of your controller communicating with one another, you're trying to bounce the information off your view layer. Don't do this. This is not what the view layer is for. When you have new information and update the collection view, update its scroll position as well.

Changing background in iOS

Hey, I have a UIImageView in my app. And I I have 4 backgrounds created for the app. I want the user to be able to change between these 4 backgrounds. I found a video on youtube to give you an idea of where I'm going.
http://www.youtube.com/watch?v=WO9NRIHU6oQ
The user clicks on a button which open a new view allowing the user to scroll thru the backgrounds and when the user got finds the backround he or she wants the user taps on a button again to use it.
Any Ideas of how I would do this?
Seems like it could be done with a parent view controller containing a button and an imageview. The button causes a secondary view to animate up. The secondary view then has theoretically any number of imageviews (but maybe just three, a previous, a current and a next which are dynamically refreshed from an array of images). Then you just have to sync the current imageview contents with the contents of the imageview in the first view in your parent view controller.
I realize a pile of text like this is probably hard to follow, especially if you're new to iPhone programming, but do some reading into uiview and viewcontroller hierarchies and you should be able to figure something out.

Resources