Suraj
Suraj

Reputation: 29

UIScrollView With next and previous button Action

i am new in iphone,i am using horizontal scrollview these are horizontally scrolling properly but i want these horizontally scrolling on previous and next button action.please help me asap.

Thanks:)

Upvotes: 0

Views: 3145

Answers (6)

soan saini
soan saini

Reputation: 260

If Any one Looking for way of doing in C# for Xamarin

    public int CurrentIndex
    {
        get => (int)Math.Round(this.scrollView.ContentOffset.X / this.scrollView.Frame.Width);
        set => this.scrollView.ScrollRectToVisible(new CGRect(new CGPoint(this.scrollView.Frame.Size.Width * value, 0), this.scrollView.Frame.Size), true);
    }

This getter and Setter should provide you current page as well let you scroll to the specified page

On Button Actions Just Update Current Index to new Values, Which could current Index +1/-1.

Upvotes: 0

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

- (IBAction)pre:(id)sender
{
    btnNext.enabled = TRUE;
    imageID--;
    [scrl_venuelist setContentOffset:CGPointMake(imageID*273, 0) animated:YES];
    pagecontrol.currentPage=imageID;
    if(imageID <= 0)
    {
        btnPrevious.enabled = FALSE;
    }
}
- (IBAction)next:(id)sender
{
    btnPrevious.enabled = TRUE;
    imageID++;
    [scrl_venuelist setContentOffset:CGPointMake(imageID*273, 0) animated:YES];
    pagecontrol.currentPage=imageID;
    if(imageID >= imageArr.count-1)
    {
        btnNext.enabled = FALSE;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; 
{
    imageID = scrollView.contentOffset.x / scrollView.frame.size.width; 
    pagecontrol.currentPage=imageID;

    if(imageID <= 0)
    {
        btnPrevious.enabled = FALSE;
        imageID = 0;
    }
    else
    {
        btnPrevious.enabled = TRUE;
    }

    if(imageID >= imageArr.count-1)
    {
        btnNext.enabled = FALSE;
        imageID = imageArr.count-1;
    }
    else
    {
        btnNext.enabled = TRUE; 
    }
}

- (void)viewDidLoad
{
    scrl_venuelist.delegate=self;
    scrl_venuelist.contentSize =CGSizeMake(273 * [imageArr count], 137);
    if(imageID == 0)
    {
        btnPrevious.enabled = FALSE;
        if(imageID == imageArr.count-1)
        {
            btnNext.enabled = FALSE;
        }
    }
    else if(imageID == imageArr.count-1)
    {
        btnNext.enabled = FALSE;
    }
    else if(imageID > imageArr.count-1)
    {
        imageID = [imageArr count]-1;
    }
    scrl_venuelist.pagingEnabled=YES;
    pagecontrol.numberOfPages=[imageArr count];
    pagecontrol.currentPage=0;
}

Upvotes: 0

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

- (void)viewDidLoad
{
    x=0;
    y=320;
}

- (IBAction)next:(id)sender 

{
    y=320;
    [scrl_Knot setContentOffset:CGPointMake(x+320,0) animated:YES];
    x+=320;

}

- (IBAction)pre:(id)sender 

{

    x=0;
    [scrl_Knot setContentOffset:CGPointMake(y,0) animated:YES];
    y-=320;

}

Upvotes: 0

VSN
VSN

Reputation: 2361

Here are the steps that was worked for me

in .h file write following code.

int scrollMove; 
UIScrollView *aScrView;

-(IBAction)nextBtnAction:(id)sender;
-(IBAction)previousBtnAction:(id)sender;

in .m file

- (void)viewDidLoad
{
[super viewDidLoad];

scrollMove=50;

aScrView=[[UIScrollView alloc]init];
aScrView.frame=CGRectMake(25,50, 270, 50);
aScrView.delegate=self;
aScrView.contentSize=CGSizeMake(1200, 0);

[self.view addSubview:aScrView];


// Do any additional setup after loading the view from its nib.
}


-(IBAction)nextBtnAction:(id)sender{

 float coordinate = 2.0f;
 //scrollMove=50;
 [aScrView setContentOffset:CGPointMake(scrollMove * coordinate, 0) animated:YES];
 scrollMove=scrollMove+50;



 }
-(IBAction)previousBtnAction:(id)sender{
float coordinate = 1.0f;


[aScrView setContentOffset:CGPointMake(scrollMove * coordinate, 0) animated:YES];
scrollMove=scrollMove-50;
}

Hope this helps you!!!

Upvotes: 1

iosdev1111
iosdev1111

Reputation: 1112

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

Upvotes: 0

sudesh
sudesh

Reputation: 144

Use this one when you want to go on next visible content of Scroll view:

(Put this one inside next button method)

float coordinate = 2.0f;
[nodeScrollView setContentOffset:CGPointMake(460 * coordinate, 0) animated:YES];

move on previous visible content view:

float coordinate = 1.0f;

[nodeScrollView setContentOffset:CGPointMake(460 * coordinate, 0) animated:YES];

(Put this one inside previous button method)

**You need some mathematical calculation with coordinate

Upvotes: 0

Related Questions