OutPut
View Controller
View
BGGridImagesView.h
BGGridImagesView.m
Using UIButtons
Alternate Code*
*( NOT A Good algorithm ) but just for example
Finally to layout codes with format in Blogger I used -
http://codeformatter.blogspot.com/
View Controller
#import "BGGridImagesView.h"
-(void) groupLayout {
BGGridImagesView *gridView = [[BGGridImagesView alloc] initWithFrame:CGRectMake(0, 0, 140, 100)];
[self.view addSubview:gridView];
}
View
BGGridImagesView.h
#import <UIKit/UIKit.h>
@interface BGGridImagesView : UIView
@end
BGGridImagesView.m
//
// BGGridImagesView.m
//
// Created by Bishal Ghimire on 8/9/13.
// Copyright (c) 2013. All rights reserved.
//
#import "BGGridImagesView.h"
@implementation BGGridImagesView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self layoutGrid];
}
return self;
}
-(void) layoutGrid {
NSInteger grid[10][10];
NSInteger k = 0;
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 5; column++) {
k++;
grid[row][column] = k;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(column*30, row * 30, 20, 20)];
NSString *string = [[NSString alloc] init];
string = [NSString stringWithFormat:@"%d",k];
lbl.text = string;
[self addSubview:lbl];
}
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
Using UIButtons
#pragma mark - Grid Layout
#define FACTOR 1.0
#define kHeight 85.0f
#define kWidth 85.0f
#define xPadding 15.0f
#define yPadding 25.0f
#define kNoOfColumn 3
-(void) loadGridImages {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
int numberOfImages = 30;
CGRect frame = self.frame;
NSInteger grid[30][30];
NSInteger k = 0;
BOOL dobreak = NO;
CGFloat heightFactor = ( kHeight / kNoOfColumn ) * 3;
CGFloat widthFactor = ( kWidth / kNoOfColumn ) * 3;
for (int row = 0; row < (numberOfImages / kNoOfColumn) && !dobreak; row++) {
for (int column = 0; column < kNoOfColumn; column++) {
k++;
grid[row][column] = k;
DLog(@"%d",k);
if (k > numberOfImages) {
dobreak = YES;
break;
}
frame.size.height = heightFactor;
frame.size.width = widthFactor;
frame.origin.x = 10 + ( heightFactor * column) + (10 * column);
frame.origin.y = 20 + ( widthFactor * row) + (10 * row);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString *string = [NSString stringWithFormat:@"%d",k];
[button setTitle:string forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:30];
//button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
button.frame = frame;
button.tag = k;
[scrollView addSubview:button];
}
}
[scrollView setContentSize:CGSizeMake(self.frame.size.width - xPadding, (((numberOfImages+1)/2) * heightFactor) * 0.8)];
[self addSubview:scrollView];
}
Alternate Code*
#define FACTOR 1.0
#define kHeight 85.0f
#define kWidth 85.0f
#define xPadding 15.0f
#define yPadding 25.0f
// Three Grid Layout
-(void) loadGridImages{
int numberOfImages = 9;
CGRect frame = self.scrollView.frame;
for (int i = 0; i < numberOfImages; i++) {
int j = 0;
if (i < 3) {
j = i+3;
} else {
j = i;
}
if (j%3 == 0 ) {
frame.origin.x = xPadding;
}
else if ((j-1)%3 == 0 ) {
frame.origin.x = xPadding * 2 + kWidth * 1 ;
} else if ((j+1) %3 == 0) {
frame.origin.x = xPadding * 3 + kWidth * 2;
}
// frame.origin.y = ((i/3) * kHeight) + yPadding * 3;
frame.origin.y = ((i/3) * kHeight) + yPadding * (i/3);
frame.size.height = kHeight * FACTOR;
frame.size.width = kWidth * FACTOR;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *myImage = [UIImage imageNamed:@"checkmark"];
[button setBackgroundImage:myImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
button.frame = frame;
button.tag = i+1;
[self.scrollView addSubview:button];
}
}
*( NOT A Good algorithm ) but just for example
Finally to layout codes with format in Blogger I used -
http://codeformatter.blogspot.com/
No comments:
Post a Comment