如何在iphone的scrollview中将中间按钮5px向上放置
假设当我滚动图库时,中间出现的按钮会比另一个按钮移动5px,所以在中间出现的任何按钮都会比其他按钮小一些。所以请帮帮我如何在iphone的scrollview中将中间按钮5px向上放置,iphone,ios,objective-c,uiscrollview,Iphone,Ios,Objective C,Uiscrollview,假设当我滚动图库时,中间出现的按钮会比另一个按钮移动5px,所以在中间出现的任何按钮都会比其他按钮小一些。所以请帮帮我 scrlview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 350, 320, 130)]; [scrlview setContentSize:CGSizeMake(600, 0)]; scrlview.delegate=self; scrlview
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];
可以使用类似for循环或while循环的循环。 让我告诉你一些关于这个的想法 在这里,我用示例代码更新了答案,更清楚地显示了
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)];
NSArray*yourray=[[NSArray alloc]initWithObjects:@“btn1”、“btn2”、“btn3”、“btn4”、“btn5”、“btn6”、“btn7”、nil];
int xPos=10;
for(int i=0;i
也许这对你有帮助。
祝你一切顺利 试试这个
for(UIButton *subview in yourScrollView.subviews)
{
CGRect subviewRect = [yourScrollView convertRect:subview.frame fromView:subview];
if(CGRectContainsPoint(subviewRect,middlePointOfScrollView))
{
// change y position of button
break;
}
}
我已经试过你的要求了。我假设按钮之间的偏移量及其宽度是相同的
- (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];
}
-(void)viewDidLoad
{
[超级视图下载];
self.scrollView.delegate=self;
[自添加按钮滚动视图];
}
-(无效)添加按钮滚动视图
{
NSInteger numberOfButtons=15;
//NSInteger可视按钮=5;
//CGFloat totalWidth=self.scrollView.frame.size.width;
CGFloat偏移=5.0f;
CGFloat按钮宽度=54.0f;/(总宽度-((可视按钮+1)*偏移量))/可视按钮;
CGFloat按钮高度=44.0f;
CGPoint原点=CGPointMake(偏移量,10.0f);
对于(int-idx=1;idx@yashesh-谢谢。但是我怎么知道哪个按钮在中间呢?@VivekSehrawat例如,你有NSArray,所以你可以指定你的array.count而不是10。而不是i==5,你可以把你的arrar.count/2。当你想要那个中间按钮时,只要做一件事,..UIButton*button=(UIButton*)[self.view-view-withtag:youraray.count/2];@VivekSehrawat我已经用示例代码更新了答案……你还有什么甜甜圈,然后告诉我……:)让我们感谢你,你能详细说明一下吗more@VivekSehrawat您需要根据滚动编号重置位置?数组元素始终相同..如何获得当前中间按钮..我的答案是动态查找中间按钮..@Anupdas-谢谢,当我滚动时,我希望一个按钮始终位于顶部(中间一个),我尝试了scrollview bt的不同代表,但没有获得顺利experience@VivekSehrawat我已编辑了上一个答案。请使用setContentOffset:animated:而不是setContentOffset:。@Anupdas-错误出现在“UISCROLLVIEW的无可见界面声明选择器setContentOffset:animated”行中,假设当我尝试didscroll委托时,他们的不是animation@VivekSehrawat有一个打字错误。我已经修复了。请现在试试。@ ANUPDAS——第一次完成了伟大的工作。通过使用ScLabVIEWDIDRONM我做了我想做的事情,BT中间按钮不在中间,在哪里改变这个东西。