Bonjour, je suis débutant sur iOS et j'ai une question.Animer plusieurs UIButtons
Je veux à nouveau organiser plusieurs boutons lors de la modification des orientations pour que j'ai ce code pour les créer:
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;
}
et le code à nouveau les organiser est:
- (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;
}
alors je fais appel lorsque l'orientation change:
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation duration: (NSTimeInterval) duration {
[self animate:btn];
Comment puis-je faire référence à chaque bouton un par un? Le code dans animate: retourne toujours button.tag = 4.
S'il vous plaît aider !!
Remerciements Bill.