UIScroll View next previous button - iphone

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];
}

Related

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;
}
}

Moving previous image in ScrollView using button click

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.

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;
}

uiscrollview into pages and move between the pages using an up and down button

I need to make my scrollview into pages and move between the pages using an up and down button.
How to achieve moving between pages with a button?
create a changePage method to call - same as you typically would with a pageControl.
I would suggest if you haven't done this before, try implementing with a pageControl and then switch to a button once you have that sorted.
-(IBAction)changePage:(id)sender {
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scroll scrollRectToVisible:frame animated:YES];
pageControlIsChangingPage = YES;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
pageControlIsChangingPage = NO;
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
-(void)scrollViewDidScroll:(UIScrollView *)_scrollView {
if (pageControlIsChangingPage) {
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}