UIScrollView next previous button - iphone

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

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.

Scroll view issue

friends
I develop one scroll view set its content size and set three view on scroll view and its scroll fine and i also want it move when segmentcontrol tap but it was not happen my view is not move on scroll can some one help me to figure out this problem.
hope i understand your question
try this
-(IBAction)SegmentControll:(id)sender
{
[myTableView removeFromSuperview];
if (SegmentControll.selectedSegmentIndex==0)
{
Yourscollview.scrollEnabled=NO;
}
if (SegmentControll.selectedSegmentIndex==1)
{
Yourscollview.scrollEnabled=YES;
}
}
if need more help ask me.
Move your scroll-view with tap on the Segment-control...Here, I am providing you sample code...you just need to decide on which segment index you want to move scroll view...
if (SegmentControll.selectedSegmentIndex==0)
{
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;
}
}
else if (SegmentControll.selectedSegmentIndex==1)
{
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 hope this will help you...

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

iPhone:UIScrollView with Paging in Landscape Mode Issue

I am build an app in Landscape mode only and I have an issue is that not fill up color with width in a Landscape mode in iPhone but I can't understand how can i do that so please give me some idea to develop this functionality.
Refer https://github.com/cwalcott/UIScrollView-Paging
Thanks in advance.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
pageControlBeingUsed = NO;
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
[super viewDidLoad];
}
- (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.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 = 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;
}
Solve my issue by just remove inner subView in Autosizing property of UIScrollView in interface Builder.
I think the reason will be
self.scrollView.frame.size returns (768,1024) in viewDidLoad: method but actually you need (1024,768). To solve this,
1) In IB, Attribute Inspecter change orientation to landscape mode.
or
2) Change
frame.size = self.scrollView.frame.size;
to
frame.size = CGSizeMake(1024,768);

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.