您好我是iOS的初学者,我有一个问题。
我想在更改方向时重新排列多个按钮,所以我有这个代码来创建它们:
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;
}
以及重新安排它们的代码是:
- (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;
}
然后我在方向改变时调用它:
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation duration: (NSTimeInterval) duration {
[self animate:btn];
我如何逐个参考每个按钮? 动画中的代码:始终返回button.tag = 4。
请协助!!
由于 比尔。
答案 0 :(得分:1)
您需要使用以下代码获取UIButtons数组:
NSArray* buttons = [springboardScrollView subviews]
这将返回scrollView中的所有子视图。如果你只有UiButtons那将是滚动视图中的所有按钮。如果在滚动视图中有其他类型的视图,则需要遍历数组并检查对象是否为UiButton,这在以下link中有说明。 然后你可以改变框架,图像,位置....