Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-01-2010, 10:54 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
JMKit is on a distinguished road
Question Using a button to scroll. Help?

Hi all, first post here and I'm just getting used to the world of iPhone SDK coding / editing.

I've got a problem I'm hoping someone can solve,

My idea is that i'd set up a UIScrollView with various images in a line, and instead of scrolling with your finger left and right, you could disable finger dragging, and instead press a button that would 'scroll' the view directly to the point you wanted to go to.


Just to clarify you'd have something like

1 2 3 4 5 - images in scroll view

then buttons representing the images

so when I pressed 'button 4' the scroll view would slide across and display that image.


Hope that all makes sense O_o

if there's anyway to do this i would appreciate the help ^_^
(oh and sorry if it's a silly question XD)
JMKit is offline   Reply With Quote
Old 05-01-2010, 11:38 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 56
aloew is on a distinguished road
Default

In my app Personal Photo Clock I use the following code in the help screen.
There is a left and a right button connected to leftPressed and rightPressed.
The scrollViewDidScroll is used to disable the buttons if left or right border is reached.

Code:
-(void) leftPressed
{
    pos -= 1;
    [scrollView scrollRectToVisible:CGRectMake(pos*320, 0, 320, 200) animated:YES];
}

-(void) rightPressed
{
    pos += 1;
    [scrollView scrollRectToVisible:CGRectMake(pos*320, 0, 320, 200) animated:YES];    
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    CGRect newBounds = aScrollView.bounds;
    
    pos = round(newBounds.origin.x / 320);
    
    if(newBounds.origin.x <= 20)
    {
        [leftButton setHidden:YES];        
    }
    else
    {
        [leftButton setHidden:NO];                
    }

    if(newBounds.origin.x >= aScrollView.contentSize.width-340)
    {
        [rightButton setHidden:YES];
    }
    else
    {
        [rightButton setHidden:NO];
    }
}
__________________
TexturePacker - the best sprite sheet maker
PhysicsEditor - editor for box2d and chipmunk collision shapes
aloew is offline   Reply With Quote
Old 05-01-2010, 12:48 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
JMKit is on a distinguished road
Default

Quote:
Originally Posted by aloew View Post
In my app Personal Photo Clock I use the following code in the help screen.
There is a left and a right button connected to leftPressed and rightPressed.
The scrollViewDidScroll is used to disable the buttons if left or right border is reached.

Code:
-(void) leftPressed
{
    pos -= 1;
    [scrollView scrollRectToVisible:CGRectMake(pos*320, 0, 320, 200) animated:YES];
}

-(void) rightPressed
{
    pos += 1;
    [scrollView scrollRectToVisible:CGRectMake(pos*320, 0, 320, 200) animated:YES];    
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    CGRect newBounds = aScrollView.bounds;
    
    pos = round(newBounds.origin.x / 320);
    
    if(newBounds.origin.x <= 20)
    {
        [leftButton setHidden:YES];        
    }
    else
    {
        [leftButton setHidden:NO];                
    }

    if(newBounds.origin.x >= aScrollView.contentSize.width-340)
    {
        [rightButton setHidden:YES];
    }
    else
    {
        [rightButton setHidden:NO];
    }
}
Hey thanks for that, I've played around with it a bit, but i'm a bit unsure as to the '-(void)' parts, I thought it needed to be an IBAction but i guess not..

Also it's giving me an error that 'pos' is undeclared O_o how should i declare it
JMKit is offline   Reply With Quote
Old 05-01-2010, 01:19 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 56
aloew is on a distinguished road
Default

My help screen consists of several images 320px width which can be scrolled by swiping left an right.
pos is an int variable which keeps the index of the screen displayed.
It is incremented/decremented if pressing left & right, updated if you scroll by swiping left and right.

-(void) is fine since IBAction is void ...
__________________
TexturePacker - the best sprite sheet maker
PhysicsEditor - editor for box2d and chipmunk collision shapes
aloew is offline   Reply With Quote
Old 05-01-2010, 05:03 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
JMKit is on a distinguished road
Default

I still can't actually get to testing the app because it fails and says the reason is: 'pos undeclared'

what am i not doing
JMKit is offline   Reply With Quote
Old 05-01-2010, 05:27 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 23
rhodesy22 is on a distinguished road
Default

I'd assume you need to declare pos in the @interface of your header file:

Code:
@interface MyView : UIView
{
	NSInteger pos;
}
rhodesy22 is offline   Reply With Quote
Old 05-01-2010, 05:35 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
JMKit is on a distinguished road
Thumbs up

Quote:
Originally Posted by rhodesy22 View Post
I'd assume you need to declare pos in the @interface of your header file:

Code:
@interface MyView : UIView
{
	NSInteger pos;
}
aaha knew it would be something like that (i.e. obvious to someone who knows lol)

well that seems fine now thanks for the speedy reply
and thanks aloew for the initial code sugestion!
JMKit is offline   Reply With Quote
Old 05-02-2010, 02:18 PM   #8 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 56
aloew is on a distinguished road
Default

sorry i did not answer earlier... but you already fixed the problem ;-)
__________________
TexturePacker - the best sprite sheet maker
PhysicsEditor - editor for box2d and chipmunk collision shapes
aloew is offline   Reply With Quote
Old 05-05-2010, 06:25 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
JMKit is on a distinguished road
Default

Quote:
Originally Posted by aloew View Post
sorry i did not answer earlier... but you already fixed the problem ;-)
hey no worries, there is a question you can answer in relation to all this though.

in your code where you talk about the position (*pos) as '320, 0, 320, 200'

what do these numbers relate too, and how would i change them to scroll up and down instead of left and right. I've tried altering the numbers to see what happens, but can't quite figure out what does what as i've ended up with some funny results (diagonal scrolling, then never scrolling back etc lol)
JMKit is offline   Reply With Quote
Reply

Bookmarks

Tags
buttons, scrolling, uiscrollview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
20 members and 309 guests
Abidullah, baja_yu, cgokey, Domele, Duncan C, Fstuff, gbenna, givensur, guusleijsten, HowEver, iphonedevshani, jbro, mdpauley, n00b, newDev, seokwon lee, SLIC, stanny, Steven.C, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,877
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:38 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0