Change Page Control With Button - iphone

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
}

Related

vertical images in UIScrollView

I tried to create a list of UIimages in a UIScrollview. When I implement it horizontally it works the way I want like:
As you see the images stick together.
Then I tried it vertically. it works but an unwanted space between the images happens like:
This is my code:
#synthesize scrollView, pageControl;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
NSArray *pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.png"],
[UIImage imageNamed:#"photo2.png"],
[UIImage imageNamed:#"photo3.png"],
[UIImage imageNamed:#"photo4.png"],
[UIImage imageNamed:#"photo5.png"],
nil];
for (int i = 0; i < pageImages.count; i++) {
CGRect frame;
//frame.origin.x = self.scrollView.frame.size.width * i;
//frame.origin.y = 0;
frame.origin.x = 0;
frame.origin.y = self.scrollView.frame.size.height * i;
frame.size = self.scrollView.frame.size;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
}
//self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * pageImages.count , self.scrollView.frame.size.height);
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width , self.scrollView.frame.size.height* pageImages.count);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageImages.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.height;
int page = floor((self.scrollView.contentOffset.y - pageWidth / 2) / pageWidth) + 1;
//CGFloat pageWidth = self.scrollView.frame.size.width;
//int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = 0;
frame.origin.y = self.scrollView.frame.size.height * self.pageControl.currentPage;
// frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
// frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
#end
in the code the horizontal code lines are commented.
Can you help me to remove this unwanted space in vertical one?
Here the image size makes the problem.
Check with this newPageView.contentMode = UIViewContentModeScaleAspectFill; instead of newPageView.contentMode = UIViewContentModeScaleAspectFit;
edit this part of your code
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
newPageView.contentMode = UIViewContentModeScaleAspectFit; // this statement is making your newPageView bigger then the image, hence spaces are coming when scrolling. !!
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
dynamically create your view "newPageView" such that the view is of exact size with your images.

dynamic UIScrollView with paging and data from NSMutableArray

I saw alot of people asking about it, I read everything but still could not figure how to do it. I have a UIScrollView with paging enabled, also I have a NSMutableArray with strings.
what I want to do is that the UIScrollView will show to UITextView with the string and when I change to the next page, it will show the next item on the NSMutableArray.
for some reason I can't get it to work.
- (void)setupPage
{
pagingView.delegate = self;
[self.pagingView setBackgroundColor:[UIColor blackColor]];
[pagingView setCanCancelContentTouches:NO];
pagingView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
pagingView.clipsToBounds = YES;
pagingView.scrollEnabled = YES;
pagingView.pagingEnabled = YES;
pagingView.backgroundColor = [UIColor clearColor];
[pagingView setShowsHorizontalScrollIndicator:NO];
[pagingView setShowsVerticalScrollIndicator:NO];
for (int i=0;i<[arrCat count];i++)
{
UITextView * txtJoke = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
[pagingView addSubview:txtJoke];
txtJoke.text = [arrCat objectAtIndex:i];
txtJoke.textAlignment = UITextAlignmentRight;
}
[pagingView setContentSize:CGSizeMake(960, [pagingView bounds].size.height)];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage) {
return;
}
/*
* We switch page at 50% across
*/
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
//pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
- (IBAction)changePage:(id)sender :(int)current
{
/*
* Change the scroll view
*/
CGRect frame = pagingView.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
[pagingView scrollRectToVisible:frame animated:YES];
/*
* When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
*/
pageControlIsChangingPage = YES;
}
This code is a combination of something from another app that i'm trying to fit into this, but without any success.
Please help. Thanks alot.
For one thing, all of your UITextView's have the same frame coordinates, so they will be on top of each other. Assuming you're scrolling horizontally, you need to offset the UITextView's x origin by the page width.
Are you using a UIPageControl? If not, you can just deleted the methods other than setupPage. Those other methods are just used for changing the UIPageControl, and changing the page using the UIPageControl.
Here is a basic scroll view implementation similar to yours:
static NSUInteger kNumberOfPages = 3;
static NSUInteger kPageWidth = 320;
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(kPageWidth * kNumberOfPages, self.scrollView.frame.size.height);
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
self.pageOne.frame = CGRectMake(0, 0, kPageWidth, self.pageOne.frame.size.height);
[self.scrollView addSubview:self.pageOne];
self.pageTwo.frame = CGRectMake(kPageWidth, 0, kPageWidth, self.pageTwo.frame.size.height);
[self.scrollView addSubview:self.pageTwo];
self.pageThree.frame = CGRectMake(2*kPageWidth, 0, kPageWidth, self.pageThree.frame.size.height);
[self.scrollView addSubview:self.pageThree];
self.pageControl.numberOfPages = kNumberOfPages;
self.pageControl.currentPage = 0;
}
# pragma mark - Scroll View Related Methods
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed_)
{
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
int page = floor((self.scrollView.contentOffset.x - kPageWidth / 2) / kPageWidth) + 1;
self.pageControl.currentPage = page;
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed_ = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed_ = NO;
}
- (IBAction)changePage:(id)sender
{
int page = self.pageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed_ = 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;
}

How can i provide Zoomin-Zoomout,Paging functionality in to UIScrollView?

I have an array of Image URl's from that array i have displayed my Images in Scrollview because i want to take the advantage of Paging and zooming so what should i do to achieve this task?suppose my array contains 10 URL then by each time scrolling i should get different image and i could also performing Zooming-In and Zooming-Out any of the 10 Images so please give me some guidelines to achieve my task.Thanks in Advance.
Try this
- (void)setCurrentPage:(NSUInteger)page {
if (page == offsetX)
return;
offsetX = page;
// in a real app, this would be a good place to instantiate more view controllers -- see SDK examples
}
- (CGSize)pageSize {
CGSize pageSize = scrollView1.frame.size;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
return CGSizeMake(pageSize.height, pageSize.width);
else
return pageSize;
}
- (void)setPagingMode {
// reposition pages side by side, add them back to the view
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imgViewArray) {
if (!view.superview)
[scrollView1 addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView1.pagingEnabled = YES;
scrollView1.showsVerticalScrollIndicator = scrollView1.showsHorizontalScrollIndicator = NO;
scrollView1.contentSize = CGSizeMake(pageSize.width * [imgViewArray count], pageSize.height);
scrollView1.contentOffset = CGPointMake(pageSize.width * offsetX, 0);
scrollViewMode = ScrollViewModePaging;
}
- (void)setZoomingMode {
NSLog(#"setZoomingMode");
scrollViewMode = ScrollViewModeZooming; // has to be set early, or else currentPage will be mistakenly reset by scrollViewDidScroll
// hide all pages except the current one
NSUInteger page = 0;
for (UIView *view in imgViewArray)
if (offsetX != page++)
[view removeFromSuperview];
scrollView1.pagingEnabled = NO;
scrollView1.showsVerticalScrollIndicator = scrollView1.showsHorizontalScrollIndicator = YES;
pendingOffsetDelta = scrollView1.contentOffset.x;
scrollView1.bouncesZoom = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// offsetX=(aScrollView.contentOffset.x)/768;
//NSLog(#"Page No:%d",offsetX);
if (scrollViewMode == ScrollViewModePaging)
[self setCurrentPage:roundf(scrollView1.contentOffset.x / [self pageSize].width)];
}
else
{
//offsetX=(aScrollView.contentOffset.x)/320;
//NSLog(#"Page No:%d",offsetX);
if (scrollViewMode == ScrollViewModePaging)
[self setCurrentPage:roundf(scrollView1.contentOffset.x / [self pageSize].width)];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView
{
NSLog(#"viewForZoomingInScrollView");
if (scrollViewMode != ScrollViewModeZooming)
[self setZoomingMode];
return [imgViewArray objectAtIndex:offsetX];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)aScrollView withView:(UIView *)view atScale:(float)scale {
NSLog(#"scrollViewDidEndZooming");
if (scrollView1.zoomScale == scrollView1.minimumZoomScale)
[self setPagingMode];
else if (pendingOffsetDelta > 0) {
UIView *view = [imgViewArray objectAtIndex:offsetX];
view.center = CGPointMake(view.center.x - pendingOffsetDelta, view.center.y);
CGSize pageSize = [self pageSize];
scrollView1.contentOffset = CGPointMake(scrollView1.contentOffset.x - pendingOffsetDelta, scrollView1.contentOffset.y);
scrollView1.contentSize = CGSizeMake(pageSize.width * scrollView1.zoomScale, pageSize.height * scrollView1.zoomScale);
pendingOffsetDelta = 0;
}
}