I've searched and searched for a tutorial for this but none of them are what I'm looking for. I've tried Apple's sample but it is just colors and I don't know how to make it views. All I'm looking for is a screen that will page while showing the page control. Each time the scroll view pages i want it to show a completely different view. Not different text or images but a different view. A lot like the home screen of the iPhone or ESPN Scorecenter app. Please Help!
Thank you.
I created this universal solution as examples found was to complicated and this is readable for me, code should be self explanatory.
- (IBAction)changePage:(id)sender {
_pageControlUsed = YES;
CGFloat pageWidth = _scrollView.contentSize.width /_pageControl.numberOfPages;
CGFloat x = _pageControl.currentPage * pageWidth;
[_scrollView scrollRectToVisible:CGRectMake(x, 0, pageWidth, _scrollView.frame.size.height) animated:YES];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (!_pageControlUsed)
_pageControl.currentPage = lround(_scrollView.contentOffset.x /
(_scrollView.contentSize.width / _pageControl.numberOfPages));
}
This does the same as #ReneDohan answer without the need for variable to store state
- (IBAction)changePage:(id)sender {
CGFloat x = self.pageControl.currentPage * self.scrollView.frame.size.width;
[self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.isDragging || scrollView.isDecelerating){
self.pageControl.currentPage = lround(self.scrollView.contentOffset.x / (self.scrollView.contentSize.width / self.pageControl.numberOfPages));
}
}
Try out this framework : https://github.com/AdrianFlorian/AFImageViewer to present images in a scroll view using a page controll to indicate the current page.
I have't added documentation yet, but you can see examples if you clone the project.
You can easily:
- download images from the internet in a separate thread by only giving an array of urls (image urls)
- give it an array of UIImage objects
- implement a delegate and manage the image for each page yourself
There is a related question: How do I use UIPageControl to create multiple views?, a really good way to do that is explained in this blog post: http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html.
I've made a demo project for this question. Please visit:
https://github.com/lenhhoxung86/PageControlDemo
Related
So what I'm trying to do is to write a simple gallery app - detail view behaves pretty much like the PhotoScroller provided by Apple in their sample code section on developer.apple.com, that is it allows the user to swipe across all the images. What I'd like to do is to update the title property on users swipe, in other words, if the user changes the image she's looking at, the self.title property should update itself to that photo's title. I know that is possible, since Apple's own Photo app is capable of displaying the current index ("1 of x"). How can this be done? I'd really appreciate any suggestions and thank you in advance.
When you scroll the ScrollView after ending the scrolling
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
method is call. So You can give the title inside this method. First Make an array of your all
Image's title. Then if your image's width is 320, then use this :-
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int index = scrollView.contentOffset.x / 320;
self.title = [titleArray objectAtIndex:index];
}
If you are putting images in a scroll view then this method will give you the page number you are currently in:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger pageNum = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
NSLog(#"page no is:%d",pageNum);
[self updateTitleForIndex: pageNum];
}
- (void)updateTitleForIndex:(NSInteger) pageNum
{
//set title using your page number
//also call this method initially for setting title
}
If you're using a paged UIScrollView, the best approach would be to do the manipulation of the title in the delegate method called
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
You should use a method of UIScrollViewDelegate protocol, such as -scrollViewDidScroll:
I need to create a main menu for my app with a UIScrollView. I have some images inside it that can be clicked. When I scroll the UIScrollView I need that on the background there are other two views that move creating a parallax effect.
Can someone provide me a sample code? I'm trying to work with
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
but I cannot find any productive example about applying on my project.
How's this? The imageView will scroll up half as fast as the UIScrollView.
float y = scrollView.contentOffset.y;
CGRect imageFrame = self.imageView.frame;
imageFrame.origin.y = y/2;
self.imageView.frame = imageFrame;
This GitHub repository has an amazing implementation that works quite well:
https://github.com/ralfbernert/RBParallaxScrolling
Here's my test of the code, using a UIScrollView with pagination (in the front) and an image in the background:
http://clrk.it/211o3h0A053m
The bit of code that does this parallax trick works as follows:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == _scrollView) {
float speedFactor = _headerImageScrollView.contentSize.width / _scrollView.contentSize.width;
[_headerImageScrollView setContentOffset:CGPointMake(speedFactor * _scrollView.contentOffset.x, 0)];
}
}
In this code, I've got a UIScrollView that contains a UIImageView; I call this _headerImageScrollView.
In front of it, I have a second UIScrollView with pagination and the three labels. That one's called _scrollView.
what I would like to do is to build a UIScrollView which contains a lots of images, the problem is that I don't want to load all the images to the scroll view at once for performance issues, so if someone can suggest and help me to build a UIScrollView which can add images on runtime, for example when I scroll left I get image and the next image is allocated when needed, paging on.
Use this delegate method for add new image.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
// called on finger up if user dragged. decelerate is true if it will continue moving afterwards
You can implement UIScrollViewDelegate and use its methods like
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
which tells you about any offset changes in the Scroll View, and then you can load whichever image should appear at that offset at run-time. For that, you would have to keep track of the current offset and the page of your scroll view.
Look into the Apple's PageControl Sample Code
This could be a good starting point
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float currPos = scroll.contentOffset.x;
BOOL movingRight = ( currPos / scroll.frame.size.width ) > 1 ? YES : NO ;
if (movingRight) {
NSLog(#"RIGHT");
[photo2 setImage:[UIImage imageNamed:#"22.jpg"]];
photo2.frame = CGRectMake(scroll.frame.size.width * 2, 0, scroll.frame.size.width, scroll.frame.size.height);
}else{
NSLog(#"LEFT");
[photo2 setImage:[UIImage imageNamed:#"33.jpg"]];
photo2.frame = CGRectMake(0, 0, scroll.frame.size.width, scroll.frame.size.height);
}
//NSLog(#"%f",currPos);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UIImage *img = [photo1 getImage];
[photo1 setImage:[photo2 getImage]];
[self.scroll scrollRectToVisible:CGRectMake(self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height) animated:NO];
[photo2 setImage:img];
}
I have a horizontal scrollview with :
[journal setPagingEnabled:YES];
I've been using a custom method to scroll to the right on the touch of a button
[journal setContentOffset:CGPointMake(320,0) animated:YES];
Is there an already existing method for working with pages and how do I find it?
Thanks,
Rd
If you're looking for something like [myScrollView scrollToPage:3], then no, there is no built in method. It's also pretty easy to roll your own if you really want something explicit. Assuming a horizontally paged UIScrollView:
- (void)scrollUIScrollView:(UIScrollView*)scrollView toPage:(NSInteger)page {
CGFloat pageWidth = scrollView.frame.size.width;
CGFloat pageHeight = scrollView.frame.size.height;
CGRect scrollTarget = CGRectMake(page * pageWidth, 0, pageWidth, pageHeight);
[scrollView scrollRectToVisible:scrollTarget animated:YES];
}
Okay, the method name is terrible, but that's the basic idea. I may be nicer in a Category on UIScrollView.
Here's a brilliant blog post on paged UIScrollView by Matt Gallagher. I used it in a project of mine, required some tweaking but otherwise was working as expected.
The UIPageControl works similar to the way that the iPhone's SpringBoard (home screen) functions.
So, I have a UIView with a UIWebView inside serving up a local HTML file (a document preview). I also have "Accept" & "Reject" UIButtons. I need these buttons to only appear after the user has scrolled to the bottom of the web page. Is there a way to capture an event for scrolling to the bottom of a UIWebView?
I have not seen any good answers for this, can anyone help?
Thanks in advance.
UIWebView conforms to UIScrollViewDelegate. As such, you can create a subclass of UIWebView (say, ScrollDetectWebView) and capture the calls to the UIScrollViewDelegate.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[super scrollViewDidScroll: scrollView];
// Whatever scroll detection you need to do
}
huh?
Why not just included the buttons on the bottom of the page?
JavaScript lets handle the scrolling event and send message to Objective-c.
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.scrollView.delegate = self;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];
CGFloat height1 = scrollView.bounds.origin.y + self.webView.bounds.size.height;
CGFloat height2 = fittingSize.height;
int delta = fabs(height1 - height2);
if (delta < 30) {
NSLog(#"HELLO!!! You reached the page end!");
}
}