How to make UIButton vertical silder menu in iphone - iphone

In my application i want to make UIButton slider with scroll view in which when we scroll uiscrollview of buttons then the button will be locate at the center please see these application's first screen sot http://itunes.apple.com/au/app/id422249255?mt=8 how can i do it and what methods i can used for it currently i am used these delegate methods of UIScrollview
-(void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
}
(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
}
(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)aScrollView
{
}

Do this:
Add these in header of .m file
#define viewWidth 40 //button width
#define viewHeight 30 //button height
#define viewOffsetX 5 //button left ie X
#define viewOffsetY 5 //button right ie Y
#define viewXspace 15 //button from top X space
#define ViewYspace 5 //button from bottom
You have images of button add them in array ie arrBtnImages
Now use these method to load button in your scrollView ie scView in viewDidLoad method
- (void)loadBtnInSlider
{
int row = 1;
int col = [arrBtnImages count] / row;
if ([mut_arrImages count] % col != 0 ) {
col++;
}
int index = 0;
for (int i=0; i < row ; i++)
{
CGRect frame;
frame.size = CGSizeMake(viewWidth, viewHeight);
frame.origin.y = (i * viewHeight) + (i * ViewYspace) + viewOffsetY;
for (int j= 0; j < col && index < [mut_arrImages count]; j++) {
CGRect frame;
frame.size = CGSizeMake(viewWidth, viewHeight);
frame.origin.x = (j * viewWidth) + (j * viewXspace) + viewOffsetX;
frame.origin.y = viewOffsetY;
UIButton *btn = [[UIButton alloc]initWithFrame:frame];
[btn setTag:j];
[btn addTarget:self action:#selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setImage:[UIImage imageNamed:[arrBtnImages objectAtIndex:j]] forState:UIControlStateNormal];
index++;
[scView addSubview:btn];
[btn release];
}
}
[scView setContentSize:CGSizeMake(col * (viewWidth+ViewYspace)+viewOffsetY,scView.frame.size.height)];
}
Use this method in vieDidLoad like this:
[self loadBtnInSlider];
Now add below selector method for button in .h file and it would be like this
-(void)btnSelector:(id)sender
{
UIButton *btnSelected = sender;
switch(btnSelected.tag)
{
// add case as much u have button
case 0:
//first button called
break;
case 1:
//second button called
break;
::
::
}
}

Related

UIScrollView + UIPageControl + UIButton

How do you make a menu like in Cut the Rope game. Image below:
I am able to make three images slide horizontally but failed to make it as a button.
Though I don't need it to be fancy as the game's, but just a simple button.
Do I need to stack three different Views with its respective buttons inside UIScrollView? I am leaning towards doing it programmatically though.
Code: http://pastebin.com/ikrJ1U7w
Its actually a UIScrollView which containing UIButton of custom type & has image on that. Below that is UIPageIndicator.
You can create it as below, althouh also search for
How to create UIButton programatically to add on scrollview
In your view controller,
In viewDidLoad, in scrollView add the buttons.
UIScrollView *scrollView = // initalize & set frame as per ur req
scrollView.userInteractionEnabled = YES;
scrollView.pagingEnabled = YES;
float x = 0.0;
float y = 0.0, width = 320.0;
float height;
for (int j = 0; j <= numberofpagesYouwant; j++)
{
CGRect frame;
UIButton *button = // Create a button at here;
frame = CGRectMake(x, y, width, height); // Use this as your button frame
[scrollView addSubview:button];
[button release];
x = x + width;
}
scrollView.contentSize = CGSizeMake(numberOfPagesYouHave * 320, heightOfYourScrollView);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if(page != activityIndicator.currentPage)
{
NSLog(#"now page change:%d",page);
[self changePageIndicator:page];
}
}
-(void)changePageIndicator:(int)index
{
activityIndicator.currentPage = index;
}
- (void)setNumberOfPages:(int)numPages1
{
activityIndicator.numberOfPages = numPages1;
}
Yes, I am pretty sure this is just several buttons placed on top of some sort of a scrollView. (most likely done programatically).

iPhone SDK: UIButtons in UIScrollView - Resize Scroll View to Fit all Non Hidden Buttons

I have 7 UIButtons in a UIScrollView. The top 3 UIButtons can be either hidden or visible depending on if statements in code. Meaning there are 8 possibilities of the 3 UIButtons displaying.
The problem i'm having is getting the UIScrollView to adjust its content size so that there are no "empty" looking spaces in it. For example if the middle UIButton is hidden but the ones above and below it are visible, there is an empty space left.
Is there any way make all the buttons fit one underneath the other in the scrollview without any gaps?
I have tried this code but it didn't work:
if([[facilities objectAtIndex:0]intValue] == 1) {
facilitiesButton.hidden = NO;
}
if([[ListingsEnabled objectAtIndex:0]intValue]==1) {
ListingsBtn.hidden = NO;
}
if([[OffersEnabled objectAtIndex:0]intValue]==1) {
OffersBtn.hidden = NO;
}
CGFloat scrollViewHeight = 0.0f;
self.DetailScrollView.showsHorizontalScrollIndicator = NO;
self.DetailScrollView.showsVerticalScrollIndicator = NO;
for (UIView* view in self.DetailScrollView.subviews)
{
if (!view.hidden)
{
CGFloat y = view.frame.origin.y;
CGFloat h = view.frame.size.height;
if (y + h > scrollViewHeight)
{
scrollViewHeight = h + y;
}
}
}
self.DetailScrollView.showsHorizontalScrollIndicator = YES;
self.DetailScrollView.showsVerticalScrollIndicator = YES;
[self.DetailScrollView setContentSize:(CGSizeMake(self.DetailScrollView.frame.size.width, scrollViewHeight))];
as #Andy commented, you need to position the actual buttons, not just change the scroll view size. A Table View may be indeed the way to go, but for 7 buttons that may be overkill. Here's a snippet that will loop through the buttons and update their positions based on visibility. I omitted the scroll view because it doesn't seem necessary, but if you need them in a scroll view that will update i can edit the code, just let me know.
/////////////////////
///// USES ARC //////
/////////////////////
- (void)viewDidLoad
{
[super viewDidLoad];
allButtons = [NSMutableArray array];
// Create 7 buttons and randomly hide some
for (int i = 0; i < 7; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 50);
[button setTitle:[NSString stringWithFormat:#"Button %i", i+1] forState:UIControlStateNormal];
[button addTarget:self action:#selector(randomizeButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.hidden = arc4random_uniform(2)-1 > 0;
[allButtons addObject:button];
}
[self updateButtonLayout];
}
-(void)updateButtonLayout
{
// position buttons based on visiblity
CGRect lastButtonFrame = CGRectMake(0, 0, 0, 0);
int verticalPadding = 10;
for(UIButton *btn in allButtons)
{
if(!btn.hidden)
{
btn.frame = CGRectMake(lastButtonFrame.origin.x, lastButtonFrame.origin.y+lastButtonFrame.size.height+verticalPadding, btn.frame.size.width, btn.frame.size.height);
lastButtonFrame = btn.frame;
}
}
}
-(void)randomizeButtons:(UIButton *)btn
{
// Randomize the visiblity of the buttons and update the interface
for(UIButton *btn in allButtons)
{
btn.hidden = arc4random_uniform(2)-1 > 0;
}
[self updateButtonLayout];
}

Dynamic UIButtons with vertical spacing on a UIScrollView

I need to space UIButtons out on a UIScrollView. The code I have works, however depending on the amount of dataItems I have, the spacing is not even.
Snippet in Question
CGRectMake(10, ((120 / (count + 1)) * (i + 1) * 3) ,300,50)
Specifically
((120 / (count + 1)) * (i + 1) * 3)
Working code
int count = [dataItems count]; /* not a specific value, can grow */
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton setFrame: CGRectMake(10,((120 / (count + 1)) * (i + 1) * 3) ,300,50) ];
[aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:#"Feed"] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:aButton];
}
Screenshot
The example on the right should look like the example on the left as far as spacing goes. The UIButtons are sitting on a UIScrollView, so the UIScrollView's contentSize should also grow if there are more dataItems so the buttons can scroll offscreen if there are say, 30+ dataItems.
sounds like you need a set size and padding...
int count = [dataItems count];
CGFloat staticX = 10; // Static X for all buttons.
CGFloat staticWidth = 300; // Static Width for all Buttons.
CGFloat staticHeight = 50; // Static Height for all buttons.
CGFloat staticPadding = 10; // Padding to add between each button.
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
//Calculate with the Static values.
//I added one set of padding for the top. then multiplied padding plus height for the count.
// 10 + 0 for the first
// 10 + 60 for the second.
// 10 + 120 for the third. and so on.
[aButton setFrame: CGRectMake(10,(staticPadding + (i * (staticHeight + staticPadding)) ,staticWidth,staticHeight) ];
[aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:#"Feed"] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:aButton];
}
however if you are trying to get buttons to do this sort of behavior. I am inclined to agree with the rest.. You can make a custom cell with a button in it.
Then you can load that button cell when the table asks for a button.
And you have the table tie to your [dataItems count] for the total of the items.
then your only calculation is to set the cell height when you initially set the dataItems count. making sure not to make them too short. and the table will handle the rest.
I agree with Gurpartap, but if your dead set on this, I'd do it a bit differently. Even if you understand the math in that setFrame statement now, will you or someone else understand it later. I sure don't looking at it quick. Why not do something like:
CGRect frm = CGRectMake(10,10,300,50);
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setFrame: frm];
frm.origin.y = frm.origin.y + aButton.frame.size.height + 10;
etc, etc
You should really use a UITableView instead. It does the scroll view, cell laying out, etc. all by itself. You just specify the cell contents.

iPhone: How to scroll random height UIView in ScrollView?

I have ScrollView and UIView created in interface builder (but can as well be code). I want to add random amount of thumbnails (subviews) to UIView and be able to scroll it up-down if there are more than the screen can take. Hovewer, I can't get ScrollView to scroll.
Where and how do I resize UIView and add it as a subview of ScrollView?
When do I set contentSize of scrollView to make it expand along with uiview? I tried creating fixed large contentSize but didn't work as well.
What properties in IB I might need to change to make it work?
I'd like to make the images in uiview clickable in next step.
I guess I could also make both view in code without IB. Just somehow can't get it to work.
Thanks in advance !
//declare base view
UIViewController *viewForLoadForm =[[UIViewController alloc] init];
viewForLoadForm.view.frame=CGRectMake(0, 0,screenSize.size.width,screenSize.size.height);
viewForLoadForm.view.backgroundColor=[UIColor blackColor];
[Manview.view addSubview:viewForLoadForm];
//declare scrollview and add to base view
UIScrollView *scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenSize.size.width,screenSize.size.height)];
scrollview.indicatorStyle=UIScrollViewIndicatorStyleBlack;
[scrollview setContentSize:CGSizeMake(screenSize.size.width,screenSize.size.height)];
scrollview.clipsToBounds = NO;
scrollview.scrollEnabled = YES;
scrollview.pagingEnabled = NO; scrollview.showsVerticalScrollIndicator =NO;
scrollview.alwaysBounceVertical= YES;
[viewForLoadForm.view addSubview:scrollview];
//Add your controls to scrollview that s image view or something which is needed to display
.........your control's code (textbox,imageview etc)
//took last control's y postion and set your K
float k=Last control's y position +100;(some thing which ll be decide scroll size)
// reassign the scrollview height
[scrollview setContentSize:CGSizeMake(screenSize.size.width,k)];
use above line for scroll view size adjustment at run time.
screenSize.size.width is 320 here
code snippet,
- (void) createThumbView
{
float y_axis = Set y axis;
int x = 0;
int totalImgs = total images;
int tempCnt = 0;
for (int i = 0; i < totalImgs;)
{
float x_axis = set x axis;
int loopCount = 0;
if (totalImgs - tempCnt >= (no of images in row))
{
loopCount = (no of images in row);
}
else
{
loopCount = totalImgs % (no of images in row);
}
for (int j = 0; j < loopCount; j++)
{
MasksData *mData = [masksList objectAtIndex:x];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x_axis, y_axis, width, height);
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png",mData.bgImage]] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = x;
[scroll View addSubview:btn];
//photoCount++;
x_axis += width + some space;
tempCnt++;
i++;
x++;
}
y_axis += height + some space;
scroll View.contentSize = CGSizeMake(320.0, y_axis);
}
}

Animate multiple UIButtons

Hi I am beginner to iOS and I have a question.
I want to re arrange multiple buttons when changing orientations so i have this code to create them :
NSArray *buttonImages = [[NSArray alloc] initWithObjects:#"button_1.png",#"button_2.png",
#"button_3.png", #"button_4.png", nil];
int xcord = 0;
//int ycord = 0;
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
screenWidth = 768;
} else {
screenWidth = 1024;
}
int buttonWidth = 57;
int buttonHeight = 86;
int screenHorizontalDivisions = screenWidth / 5;
for (int i=0; i<4; i++) {
int buttonStartingOffset = screenHorizontalDivisions - buttonWidth / 2;
btn = [[DashButton alloc] initWithFrame:CGRectMake(buttonStartingOffset + xcord, 40, buttonWidth,buttonHeight) andImage:[buttonImages objectAtIndex:i] andTitle:#"Test"];
btn.tag = i+1;
[springboardScrollView addSubview:btn];
//[btn release];
xcord = xcord + screenHorizontalDivisions;
}
and the code to re arrange them is:
- (void)animate: (id)sender {
UIButton *button = (UIButton *)sender;
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
screenWidth = 768;
} else {
screenWidth = 1024;
}
int xcord = 0;
int buttonWidth = 57;
int buttonHeight = 86;
int screenHorizontalDivisions = screenWidth / 5; //find the divisions
int buttonStartingOffset = screenHorizontalDivisions - buttonWidth / 2; //center the button
NSLog (#"%i", button.tag);
switch (button.tag) {
case 1:
button.frame = CGRectMake(buttonStartingOffset, 40, buttonWidth,buttonHeight);
break;
case 2:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions, 40, buttonWidth,buttonHeight);
break;
case 3:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions * 2, 40, buttonWidth,buttonHeight);
break;
case 4:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions * 3, 40, buttonWidth,buttonHeight);
break;
}
then I am calling it when orientation changes:
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation duration: (NSTimeInterval) duration {
[self animate:btn];
How can I refer to each button one by one?
The code in animate: always return button.tag=4.
Please assist!!
Thanks
Bill.
You need to get the array of UIButtons with the following code:
NSArray* buttons = [springboardScrollView subviews]
This will return all the subviews in the scrollView. If you have UiButtons only that will be all buttons in the scroll view. If you have other types of views in scroll view then you need to iterate through array and check if the object is UiButton, which is explained on the following link .
Then you can change the frame, image, position....