How to implement a cyclic UIScrollView? - iphone

How to implement a cyclic UIScrollView? That is to say, when you scroll to the very left item then the UIScrollView will show the very right one. Any help would be appreciate.

Sure, you need three views. At any given time you have a left view, a right view and a current view.
This requires notification of each movement through the UIScrollViewDelegate.
If you detect that you moved right, you free left, make left = current, current = right, and make a new right.
If you detect that you moved left, you free right, make right = current, current = left, and make a new left.
Generally speaking, any view that is more than one page away from current is not required. So you need only three pages in total.
Of course you also need to manipulate the position of the UIScrollView so you can make the movements - the net result is you don't move although it looks like you have. When you have done the scroll, and altered the views according to the left/current/right shuffle - you do
[self scrollRectToVisible:(middle frame) animated:NO];
so that you are always looking at the same actual page, with one page each side of it. When the scroll happens it looks like the user can keep scrolling around in a loop - but after each page ticks over, the views are shuffled, the position within the scroll view gets set back to the middle and the user can scroll again.
Getting back to the start is simply a matter of using the view related to whatever object is at the other end of whatever data structure you are using - so if current = [(NSArray)data lastObject] then right = [(NSArray)data objectAtIndex:0].

Related

How can I animate this custom view transition in my iPhone app?

This is a tricky question to explain, but basically I have an app with a home screen with a bunch of circular icons on it. When the user taps on an icon, it pushes a table view onto the navigation stack. When an icon is tapped on the home screen, I want the tapped icon to stay on top of the table view as it scrolls in, and then drop onto the table view in the cell that represents it. All the other icons, however, should stay below the incoming table view, like normal. Here's a quick diagram I whipped up...
I'm about to begin researching this, I just wanted to quickly post this question in case anyone has some ideas about how I can approach this. Thanks so much for your wisdom!
Oh thats soo easy!!!
I'll try to explain my solution:
You need two sets of icons.
One set lays on Home View, second is exact location, but as a subview on main window (by default - hidden)
Once You select a button, and want to push a list - You un-hide corresponding window subview icon, and with delay - animate / shrink it to tableView corresponding location.
Once animation is finished - hide icon (instant hide will not be noticeable, as this icon will be positioned/scaled to precisely reassemble icon in tableview), and move it to default position/scale (still hidden).
That's it.
This is pretty simple, and can be done without adding second instances of your buttons. You'll use the view hierarchies z-index for this.
When you add the table view as a subview of the main view don't use addSubview, use:
[self.view insertSubview:table belowSubview:button];
Button^ will represent the button that you want to stay on top, and for your other buttons, you just have to make sure that they are lower on the stack. This way, when the table flies in, it can keep going past the button but the button will stay above it.
You can also use:
[self.view insertSubview:table atIndex:5];
On all of these buttons and the table to have total control over each objects z-index.

Swipe through UITableView results

What I do now:
I have a UITableView that displays results from a search query, when I touch a result in the TableView it loads a subview that displays details of that result. I can swipe across that subview to switch between results like next and previous. All it does is change out the displayed data but it uses the same view
What I want to do:
I want it to show an animation when swiping between results, similar to the photos app, however since the number of results can be quite large I seem to be having issues figuring out a simple way to switch views when swiping without loading a new view for each result to start with.
Does anyone know of an easy way to do this? Perhaps Apple has already made this easy and I am just unaware.
Thanks in advance.
Instead of instantiating a new view for each result, you could have an array with a few views (3 for example), and reuse them, kinda like the UITableView. When you swipe, you retrieve from your array a view that is not visible on the screen, replace its content, and display it. I guess at the end of your animation, one view will get out of the screen, so it will be available for reuse later on.
A second idea, which is a little more complex and tricky to code, is to use a UITableView as your subview, rotate it by 90 degrees (so it scrolls horizontally), and rotate all the views you put in its cells by -90 degrees. Then you'll benefit from the tableview's reuse mechanism.

How does the UIScrollView delegate know about my UIScrollView ivar?

I'm working on a app that has a paging UIScrollView that contains three scrollViews as subviews which display images that you can pinch and zoom on. The code came from Apple's "PhotoScroller" sample code but I've stripped out the tiling functionality because I'm trying to master the basics first.
I can zoom in on my different images and page through them, which is great. When I leave an image that has been zoomed in to go to the next "page" of the pagingScrollView I want it to be restored to it's original zoom size, right now it stays at the zoom level that I left it at.
I imagine I need to write some code that tells the zoomed UIScrollView to go back to the appropriate zoom scale when I get to the next page. My question is how do I do that?
So far I've been working with UIScrollView's Delegate method "scrollViewDidScroll" and and the "contentOffset" property. Another thought is somehow getting a notification that the pagingScrollView has moved to the next "page."
Any ideas?
If you're interested in knowing when the scroll view has stopped (e.g. the user has let go and the scroll view has snapped into position on a new page) you can use scrollViewDidEndDecelerating:. However, the user may scroll several pages before allowing the scroller to fully stop, so the last page the user stopped on may not be the immediately preceding or immediately following page. You can keep a pointer to the current page and each time the user stops on a page restore the previously current page to the default zoom level before updating the current page pointer.

Objective C (iphone/ipad) - carousel coding best practice

I need to build a scroll view (with 10 items for example) that can be continuously scrolled (with paging enabled). In other words as you keep scrolling to the right, you see the item you started with once to get to the end, and it just keeps looping.
I'm looking for recommendations on how to a approach this. I will be receiving an array of images. I can lay them out next to each other no problem. My main concern is how/when to move the images so they keep appearing in a loop as you scroll. Thanks!
Append the first picture as the last picture. Just as you move further right from the last picture, jump to the first.
And vice versa of course.
Use -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; delegate to know when someone's moving through your scroll view.
When you get to the last page you need to move the first image view in the scroll view to the end. ie:
for(UIImageView *image in myscrollview){
if(image.frame.origin.x == 0){
// move to the end...
}
else{
// move your image a scroll view's width to the left...
}
}
obviously it will be much more complicated than this, you will have to work out which way the user is scrolling and sort it out like that, but this gives you a basis to start with. :)

iPhone: scroll view with arbitrary page/"settling" boundaries?

I'm trying to figure out if I can get what I want out of UIScrollView through some trickery or whether I need to roll my own scroll view:
I have a series of items in row that I want to scroll through. One item should always be centered in the view, but other items should be visible to either side. In other words, I want normal scrolling and edge bouncing, but I want the deceleration when the user ends a touch to naturally settle at some specified stop point. (Actually now that I think of it, this behavior is similar to coverflow in this respect.)
I know UIScrollView doesn't do this out of the box, but does anyone have suggestions for how it might be made to do this, or if anyone's spotted any code that accomplishes something similar (I'm loathe to reimplement all the math for deceleration and edge bounce)
Thanks!
There is not a whole lot of trickery to this. Just use an UIScrollView with paging enabled. Make it the size of one of your items, and locate it where you want that item to appear. Next, disable the "Clip Subviews" option on the scroll view (either in IB, or programmatically), and you are all set.