Reputation: 6570
Like suppose when i scroll the gallery the button which comes in the middle will shift 5px up than the other So whatever button comes up in the middle it will be little up than other. So please help me
scrlview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 350, 320, 130)];
[scrlview setContentSize:CGSizeMake(600, 0)];
scrlview.delegate=self;
scrlview.backgroundColor=[UIColor blueColor];
scrlview.scrollsToTop=NO;
[self.view addSubview:scrlview];
btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame=CGRectMake(10, 10, 55, 50);
[btn1 setTitle:@"Btn1" forState:UIControlStateNormal];
[scrlview addSubview:btn1];
btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame=CGRectMake(70, 10, 55, 50);
[btn2 setTitle:@"Btn2" forState:UIControlStateNormal];
[scrlview addSubview:btn2];
btn3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame=CGRectMake(130, 10, 55, 50);
[btn3 setTitle:@"Btn3" forState:UIControlStateNormal];
[scrlview addSubview:btn3];
btn4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn4.frame=CGRectMake(190, 10, 55, 50);
[btn4 setTitle:@"Btn4" forState:UIControlStateNormal];
[scrlview addSubview:btn4];
btn5=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn5.frame=CGRectMake(250, 10, 55, 50);
[btn5 setTitle:@"Btn5" forState:UIControlStateNormal];
[scrlview addSubview:btn5];
btn6=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn6.frame=CGRectMake(310, 10, 55, 50);
[btn6 setTitle:@"Btn6" forState:UIControlStateNormal];
[scrlview addSubview:btn6];
btn7=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn7.frame=CGRectMake(370, 10, 55, 50);
[btn7 setTitle:@"Btn7" forState:UIControlStateNormal];
[scrlview addSubview:btn7];
btn8=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn8.frame=CGRectMake(430, 10, 55, 50);
[btn8 setTitle:@"Btn8" forState:UIControlStateNormal];
[scrlview addSubview:btn8];
btn9=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn9.frame=CGRectMake(490,10, 55, 50);
[btn9 setTitle:@"Btn9" forState:UIControlStateNormal];
[scrlview addSubview:btn9];
Upvotes: 2
Views: 330
Reputation: 10201
I have tried out your requirement. I have assumed that the offset between buttons and their width is same.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
[self addButtonsToScrollView];
}
- (void)addButtonsToScrollView
{
NSInteger numberOfButtons = 15;
//NSInteger visibleButtons = 5;
//CGFloat totalWidth = self.scrollView.frame.size.width;
CGFloat offset = 5.0f;
CGFloat buttonWidth = 54.0f;//(totalWidth - ((visibleButtons+1)*offset))/visibleButtons;
CGFloat buttonHeight = 44.0f;
CGPoint origin = CGPointMake(offset, 10.0f);
for (int idx = 1; idx<=numberOfButtons; idx++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect buttonFrame = CGRectZero;
buttonFrame.origin = origin;
buttonFrame.size = CGSizeMake(buttonWidth, buttonHeight);
button.frame = buttonFrame;
NSString *titleString = [NSString stringWithFormat:@"Title%d",idx+1];
[button setTitle:titleString forState:UIControlStateNormal];
button.tag = idx;
[self.scrollView addSubview:button];
origin.x+=buttonWidth+offset;
}
[self.scrollView setContentSize:CGSizeMake(origin.x, 200.0f)];
}
- (void)adjustCenterButton
{
//content point is set in such a way that there would be exaclty 5 button visible all the time
CGPoint contentOffset = self.scrollView.contentOffset;
NSUInteger units = floorf(contentOffset.x/59.0f); //buttowidth+offset
contentOffset.x = units*59.0f;
[self.scrollView setContentOffset:contentOffset animated:YES];
NSArray *subViews = [self.scrollView subviews];
//Finds the tag of button which would be the center button
//since the visible buttons are 5, 3 is added,
NSInteger selecteButtonIndex = floorf(contentOffset.x/(59.0f))+3;
for (UIView *button in subViews)
{
//Just a bypass to remove the scroll indicator
if (button.tag!=0)
{
CGRect frame = button.frame;
frame.origin.y = (button.tag == selecteButtonIndex)?5.0f:10.0f;
button.frame = frame;
}
}
CGSize contentSize = self.scrollView.contentSize;
contentSize.height = 200.0f;//self.scrollView.frame.size.width;
self.scrollView.contentSize = contentSize;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
/*The update is only called when the scrollView is not decelerating.
The method is an required if the user is scrolling very slowly,
the other scrollViewDidEndDecelerating: will not be invoked*/
if(!decelerate)
{
[self adjustCenterButton];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self adjustCenterButton];
}
Upvotes: 4
Reputation: 1752
You can use of loop like for loop or while loop. Let me give you some idea about that.
Here i have updated answer with sample code with more clearly
NSArray *yourArray = [[NSArray alloc] initWithObjects:@"btn1",@"btn2",@"btn3",@"btn4",@"btn5",@"btn6",@"btn7", nil];
int xPos = 10;
for (int i = 0 ; i < yourArray.count; i ++) {
int yPos = 30;
if (i == (yourArray.count /2)) {
yPos = 25;
}
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(xPos, yPos, 55, 50);
button.tag = i;
[button setTitle:[yourArray objectAtIndex:i] forState:UIControlStateNormal];
[scrollView addSubview:button];
xPos+=55;
}
[scrollView setContentSize:CGSizeMake(xPos+10, 55)];
// To get Center Button
UIButton *centerButton = (UIButton *)[scrlview viewWithTag:(yourArray.count /2)];
May be this will be help you. All the best !!!
Upvotes: 4
Reputation: 3905
Try this,
for(UIButton *subview in yourScrollView.subviews)
{
CGRect subviewRect = [yourScrollView convertRect:subview.frame fromView:subview];
if(CGRectContainsPoint(subviewRect,middlePointOfScrollView))
{
// change y position of button
break;
}
}
Upvotes: 2