UICollection view update callback - ios

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.

Related

Creating a menu in iOS

I'm currently creating an update of my iOS application and I'm a bit stuck. I've tried to googling around but cannot find a decent answer on this.
I've a menu which links to different views. And I'm not really sure if I've done it the best method.
I've created a view, and added the links into a stack view. Should I considering changing it all to a tableview? or a collection view? Or maybe there's another way?
The current look:
Should I change this to a tableview? collection view? or something else? Or just let it stay as it is?
If the number of items in your menu changes at runtime and is large, you should use a table view, because a table view is good for efficiently displaying a screen's worth of items from a large list of items.
If the contents of your menu is small (under maybe two screenfuls of items) and fixed at compile time and you are using a storyboard, then you could use a table view with static cells, if you can make it look the way you want.
If the contents of your menu is small, then you can use a stack view (inside a scroll view) if that is easier for you. There is no particular advantage to using a table view over a stack view to display a small amount of content, unless you need other features of the table view (like the ability to select/deselect rows).
Based on the screen shot you posted, I'd either use a table view with static cells (since the screen shot is from a storyboard) or a stack view, depending on whether I can get the appearance I want from a table view. If, as in the screen shot, the buttons must be centered vertically, I'd use a stack view, because it's easier to vertically center the content with a stack view.
Look, the fact of have many itens on your screen is clear on the mobile applications, to make it easy, we have collecions view like UITableView and UICollectionView. On the UITableView's case, this implements the scrolling and have methods do handle the operations' list, you can see the documentation to check these methods: https://developer.apple.com/documentation/uikit/uitableview.
Main reasons to use UITableView
Implements scroll behavior.
Independent of size screen you can access all itens.
Easy to detect interactions like tap on cell.
Easy to make changes, like insert and remove content.
The UITableView exists precisely to solve problems like you has.

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.

Automatically scroll to a subview in a scrollview

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.

ios - Making UIScrollView scroll smoothly

In my iPad application, i've a scroll view that lists images of 20 video albums. The list of 20 images will be sent by server.
Now when the user reaches end of scroll view, i've to send a request to get next 10 videos details.
After fetching the data, i want to add them to the scroll view at the end. But I want to delete the 10 videos at the beginning. So that at a time scroll view will show only 20 video details all the time.
When user scrolls again to the end, i'll send request to server, get next 10 video details and add them to the scroll view, remove 10 beginning video details from scroll view...... and similarly when user scrolls to the beginning i'll fetch 10 previous videos and repeat the same.
Here i want to make sure the scrolling must be smooth and don't want to have flickers in scroll view. can some one suggest to have better scroll view coding to achieve my requirements please?
If your code contains scroll.pagingEnabled = YES; Then please remove this part of code.
It will surely work.
The real problem here will be in setting up cell recycling. It's not that hard to do, I have a couple examples of which you can look at, one being here. You'll need to make some changes to that code of course, it is set up in "pages" — Only one view on screen at any one time, and I don't think that's what you want.
Aside from that...
Your critical part here will be in designing your data source in a way that allows removing of old items, and adding new items, without skipping items.
For instance, the way I might approach this is to render the "last" item in your scroll view, the same as you want the first item in the scrollview to be. When the user gets to the last item, set your content offset to the start of the scrollview without animation (haven't actually tried this, so there may be a jolt that you don't want, test it and see). This will give the appearance of endless scrolling. Your data source would simply overwrite old indexes when adding new items, instead of adding to the end of an array for instance.
If you are presenting then in the form of a grid view, I suggest you take a look at AQGridView. That grid view supports cell queueing and dequeueing, which does exactly what you describe.
You can also use the delegate method scrollViewDidScroll: to determine if you're at the bottom of the scrollview and trigger the download of the next set of albums.

Resources