Moving previous image in ScrollView using button click - iphone

When I press previous button it does not take any effect. Next Button is working properly but previous button do not show previous image. I am using scroll view to load images:
- (IBAction)next{
int arraycount = [appDelegate.articles count];
for (int nextIndex = [pageControl currentPage]; nextIndex < arraycount; nextIndex++) {
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}
-(IBAction)previousButtonAction
{
int arraycount = [appDelegate.articles count];
NSLog(#" arraycount : %d", arraycount);
NSLog(#" [pageControl currentPage] : %d", [pageControl currentPage]);
for (int nextIndex = [pageControl currentPage]; nextIndex>arraycount; nextIndex--) {
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}

Okay, I now think I would approach this differently. I would check the current offset before re-assigning it.
untested code:
- (IBAction)next{
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}

I would disable the "next" button when the user gets to the third image. If you prefer to return to the first image when the user presses "next" in the third image, then you must add an additional else after your if to indicate that case:
- (IBAction)next{
int arraycount = [appDelegate.articles count];
for (int nextIndex = [pageControl currentPage]; nextIndex < arraycount; nextIndex++) {
if (nextIndex < arraycount) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * nextIndex;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
// If the user clicks on next in the last item, return to the first
else {
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}

arraycount = 3
[pageControl currentPage] is zero indexed so when you are on page 3 it equals 2.
so the start of your for loop nextIndex equals 2 which is less than arrayCount so it gets into your loop.
change it to:
for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) {
okay what does this output:
- (IBAction)next{
int arraycount = [appDelegate.articles count];
NSLog(#" arraycount : %d", arraycount);
NSLog(#" [pageControl currentPage] : %d", [pageControl currentPage]);
for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) {
NSLog(#" nextIndex : %d", nextIndex);
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * nextIndex;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}

Fo me it is working in swift 3.
func prevAction (sender:UIButton)
{
print("self.scView.contentOffset.x ..\(self.scView.contentOffset.x)")
if ( self.scView.contentOffset.x <= self.scView.contentSize.width )
{
var frame = CGRect()
frame.origin.x = self.scView.contentOffset.x - 80;
frame.origin.y = 0;
frame.size = self.scView.frame.size;
self.scView .scrollRectToVisible(frame, animated: true)
}
}
Thanks to #ader.

Related

How do I change the current page for Ray Wenderlich's UIScrollView Tutorial?

I am using Ray Wenderlich's (Matt Galloway's) tutorial on UIScrollViews:
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
I am using the section, "Paging with UIScrollView" in my app. This works great when you always want the page to load on 0, but I would like the current page to be dynamic. I will be passing this information in based on which thumbnail the user picked from the previous view controller, which is an NSInteger called "listIndex."
How do I change the current page to my listIndex? I have tried changing
self.pageControl.currentPage = 0;
to
self.pageControl.currentPage = self.listIndex;
in viewDidLoad, but this doesn't seem to help - possibly because it's getting reset in loadVisiblePages?
Thank you so much for the help! Here's the code:
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableString *myHTML = [[NSMutableString alloc] initWithString:#"<html><head><style type='text/css'> p {font-family:sans-serif; text-shadow: 3px 4px 5px #000; color:#FFF; position: absolute; bottom:0; font-size:10pt; font-weight:bold; margin-bottom:5px;}</style><body><p>"];
[myHTML appendString:self.caption];
[myHTML appendString:#"</p></body></html>"];
[captionWebView loadHTMLString:myHTML baseURL:nil];
self.pageImages = [NSMutableArray array];
for (int i=0; i < [imageList count]; i++) {
NSString *path = [(Image *)[imageList objectAtIndex:i] path];
[self.pageImages addObject:[UIImage imageNamed:path]];
}
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
//self.pageControl.currentPage = self.listIndex; //Tried this
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.frame = CGRectMake(0, 0, 320, 468);
} else {
self.scrollView.frame = CGRectMake(0, 0, 320, 380);
}
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}
- (void)viewWillDisappear:(BOOL)animated {
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.contentSize = CGSizeMake(320, 468);
} else {
self.scrollView.contentSize = CGSizeMake(320, 380);
}
}
Try setting the scrollview's initial contentOffset in viewWillAppear::
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the content size of the scroll view
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// Set initial content offset of scrollview
self.scrollView.contentOffset = CGPointMake(pagesScrollViewSize.width * self.listIndex, self.scrollView.contentOffset.y);
// Load the initial set of pages that are on screen
[self loadVisiblePages];
}

Change Page Control With Button

I have a simple page control set up with two different pages. I have a button called nextButton, and if the user hits that button, I want to change the page and scroll to the next page.
My page control works, and I can scroll to change pages, but the button does not work at all. It is getting called though.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * TUTORIALNUMBEROFPAGES, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
self.pageControl.numberOfPages = TUTORIALNUMBEROFPAGES;
self.pageControl.currentPage = 0;
CGRect pageFrame = self.scrollView.frame;
pageFrame.origin.x = pageFrame.size.width * 0;
pageFrame.origin.y = 0;
self.pageOne.frame = pageFrame;
[self.scrollView addSubview:self.pageOne];
pageFrame.origin.x = pageFrame.size.width * 1;
self.pageTwo.frame = pageFrame;
[self.scrollView addSubview:self.pageTwo];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed) {
return;
}
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
NSInteger currentPage = self.pageControl.currentPage;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
[self changePage:self];
}
Try something like this:
- (IBAction)nextPage:(id)sender
{        
    NSInteger currentPage = self.pageControl.currentPage + 1;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
  [self nextPage:self];
}
Notice the +1 in that code. Before, you were literally setting the content offset exactley equal to the original offset because you were just taking the value of the page control.
You can try with this:
- (IBAction)changePage:(id)sender
{
NSInteger currentPage = self.pageControl.currentPage;
if([sender isKindOfClass:[UIButton class]){
currentPage+=1;
}
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
[self changePage:sender];//if this action is attached just to your button
}

UIScrollView next previous button

next button is only working for 2 images i have 10 images in array after two images it does not move forward previous button is working for all images but next button only work up to one and second image
Below are the next Button Code
- (IBAction) nextButtonAction {
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x +self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
// Here is the previous button code
-(IBAction)previousButtonAction
{
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x -self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
Adding images to the data base
-(IBAction)addToCollectionButtonAction{
GeoNewsAppDelegate *appDelegate = (GeoNewsAppDelegate *)[[UIApplication sharedApplication] delegate];
// Create a Coffee Object.
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:0];
int pageNumber = [pageControl currentPage];
NSLog(collectionImage);
RowTwo*aRowTwo=[appDelegate.articles objectAtIndex:pageNumber];
NSString*thumb2=aRowTwo.image;
coffeeObj.thumb = thumb2;
coffeeObj.path = thumb2;
[appDelegate addCoffee:coffeeObj];
}
Replace self.scrollView.frame.size.width With self.scrollView.contentSize.width
- (IBAction) nextButtonAction {
if ( self.scrollView.contentOffset.x <= self.scrollView.contentSize.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x +self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
// Here is the previous button code
-(IBAction)previousButtonAction
{
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x -self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
- (IBAction) nextButtonAction {
if ( self.scrollView.contentOffset.x < (self.scrollView.frame.size.width*10) ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x +self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}

UIScroll View next previous button

I have a scroll view. Inside scroll view, I have image view. I want to go next and previous image by button click. In below it works on only three images of array not more than that so how to work for all. I have 70 images in array
- (IBAction)nextButtonAction {
int arraycount = [appDelegate.articles count];
NSLog(#" arraycount : %d", arraycount);
NSLog(#" [pageControl currentPage] : %d", [pageControl currentPage]);
for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) {
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}
-(IBAction)previousButtonAction{
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x - self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
- (IBAction)nextButtonAction {
int arraycount = [appDelegate.articles count];
if ( self.scrollView.contentOffset.x < (self.scrollView.frame.size.width *arraycount) ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
- (IBAction)btn_NextPressed:(id)sender
{
CGFloat pageWidth = self.scrollview.frame.size.width;
intPage = (NSInteger)floor((self.scrollview.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
[self.scrollview setContentOffset:CGPointMake(self.scrollview.contentOffset.x + self.scrollview.contentSize.width/numberOfPages ,0) animated:YES];
}
- (IBAction)btn_PrevPressed:(id)sender
{
CGFloat pageWidth = self.scrollview.frame.size.width;
intPage = (NSInteger)floor((self.scrollview.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
[self.scrollview setContentOffset:CGPointMake(self.scrollview.contentOffset.x - self.scrollview.contentSize.width/numberOfPages ,0) animated:YES];
}

program received signal exc_bad_access at scrollViewDidScroll

In my app I tried to do a scrollView with a pageController. The pageController is working but the scrollView is not working. When I try to go to next page I always get program received signal exc_bad_access at line [self loadScrollViewWithPage:page];
from method scrollViewDidScroll
I can't see where is my mystake. Please look at this code...
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.contentSize = CGSizeMake(vosvehiculeScrollView.frame.size.width * pageControlVehiculePossedee.numberOfPages, vosvehiculeScrollView.frame.size.height);
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
for (int i=0; i<pageControlVehiculePossedee.numberOfPages;i++){
[self loadScrollViewWithPage:i];
}
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
CGRect frame = vosvehiculeScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
viewVehicules.frame = frame;
[vosvehiculeScrollView addSubview:viewVehicules];
NSLog(#"%d",page);
}
- (void) scrollViewDidScroll: (UIScrollView *) sender {
if (pageControlUsed) {
return;
}
CGFloat pageWidth = vosvehiculeScrollView.frame.size.width;
int page = floor((vosvehiculeScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControlVehiculePossedee.currentPage = page;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView {
pageControlUsed = NO;
}
- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {
pageControlUsed = NO;
}
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
CGRect frame;
frame.origin.x = self.vosvehiculeScrollView.frame.size.width * self.pageControlVehiculePossedee.currentPage;
frame.origin.y = 0;
frame.size = self.vosvehiculeScrollView.frame.size;
[self.vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}