3

我对动画一无所知,我已经使用了很多建议的动画,但没有按照我的要求工作。我有一个根据字长显示的 UIButtons 序列,即每个按钮都包含一个字符那个词。所以我想做的是在放置词本身时,我希望按钮来自右侧并回到它们提到的位置(坐标)。这是我创建等于词长的按钮的方法。

-(void)buttonsCreation:(int)noOfButtons
{
    for (UIView *view in self.view.subviews)
    {
        if ([view isKindOfClass:[UIButton class]])
        {
            if (view.tag >=100)
            {
                [view removeFromSuperview];
            }
        }
    }

    NSMutableArray *charactersArray = [NSMutableArray array];
    self.wordButtons = [[NSMutableArray alloc]initWithCapacity:30];

    BOOL record = NO;
    int randomNumber;

    for (int i=0; [charactersArray count] < jumbledWord.length; i++) //Loop for generate different random values
    {
        randomNumber = arc4random() % jumbledWord.length;//generating random number
        if(i==0)
        {
            [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
        }
        else
        {
            for (int j=0; j<= [charactersArray count]-1; j++)
            {
                if (randomNumber ==[[charactersArray objectAtIndex:j] intValue])
                    record = YES;
            }
            if (record == YES)
                record = NO;
            else
                [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
        }
    }

    int arrayValue;
    float x = 60.0;
    float y = 50.0;
    for(int i=0;i<[jumbledWord length];i++)
    {
        if(i>=0 && i<10)
        {
            arrayValue = [[charactersArray objectAtIndex:i] intValue];
            x = x + 37;
            UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            characterButton.frame = CGRectMake(x,175.0, 35.0, 35.0);

            [wordButtons addObject:characterButton];
            NSString *characterValue = [NSString stringWithFormat:@"%c",[jumbledWord characterAtIndex:arrayValue]];
            [characterButton setTitle:characterValue forState:UIControlStateNormal];
            [characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (arrayValue==0)
            {
                arrayValue = 100;
            }
            [characterButton setTag:arrayValue*100];
            [self.view addSubview:characterButton];
        }
        else if(i>=10 && i<20)
        {
            arrayValue = [[charactersArray objectAtIndex:i] intValue];
            y = y + 37;
            UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            characterButton.frame = CGRectMake(y,200.0, 35.0, 35.0);

            [wordButtons addObject:characterButton];
            NSString *characterValue = [NSString stringWithFormat:@"%c",[jumbledWord characterAtIndex:arrayValue]];
            [characterButton setTitle:characterValue forState:UIControlStateNormal];
            [characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (arrayValue==0)
            {
                arrayValue = 100;
            }
            [characterButton setTag:arrayValue*100];
            [self.view addSubview:characterButton];
        }
    }
}

PS -* Off post *:我正在使用下面的代码从视图中关闭按钮序列,然后带来下一个按钮序列,即下一个单词

for (UIView *view in self.view.subviews)
{
    if ([view isKindOfClass:[UIButton class]])
    {
        if (view.tag >=100)
        {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:1.30f];
            view.transform =
            CGAffineTransformMakeTranslation(
                                             view.frame.origin.x,
                                             480.0f + (view.frame.size.height/2)  // move the whole view offscreen
                                             );
            [UIView commitAnimations];
        }
    }
}

有人可以指导我吗,谢谢:)

4

1 回答 1

3

视图控制器.m

@interface ViewController ()

@property(nonatomic,strong) NSMutableArray *wordButtons;
@property(nonatomic,strong) NSString *jumbledWord;

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.jumbledWord = @"NORBERT";
    [self buttonsCreation:7];
    [self animation];
}

-(void)buttonsCreation:(int)noOfButtons
{
    for (UIView *view in self.view.subviews)
    {
        if ([view isKindOfClass:[UIButton class]])
        {
            if (view.tag >=100)
            {
                [view removeFromSuperview];
            }
        }
    }

    NSMutableArray *charactersArray = [NSMutableArray array];
    self.wordButtons = [[NSMutableArray alloc]initWithCapacity:30];

    BOOL record = NO;
    int randomNumber;

    for (int i=0; [charactersArray count] < self.jumbledWord.length; i++) //Loop for generate different random values
    {
        randomNumber = arc4random() % self.jumbledWord.length;//generating random number
        if(i==0)
        {
            [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
        }
        else
        {
            for (int j=0; j<= [charactersArray count]-1; j++)
            {
                if (randomNumber ==[[charactersArray objectAtIndex:j] intValue])
                    record = YES;
            }
            if (record == YES)
                record = NO;
            else
                [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
        }
    }

    int arrayValue;
    float x = 60.0;
    float y = 50.0;
    for(int i=0;i<[self.jumbledWord length];i++)
    {
        if(i>=0 && i<10)
        {
            arrayValue = [[charactersArray objectAtIndex:i] intValue];
            x = x + 37;
            UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            characterButton.frame = CGRectMake(x,175.0, 35.0, 35.0);

            [self.wordButtons addObject:characterButton];
            NSString *characterValue = [NSString stringWithFormat:@"%c",[self.jumbledWord characterAtIndex:arrayValue]];
            [characterButton setTitle:characterValue forState:UIControlStateNormal];
            [characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (arrayValue==0)
            {
                arrayValue = 100;
            }
            [characterButton setTag:i+1];
            [self.view addSubview:characterButton];
        }
        else if(i>=10 && i<20)
        {
            arrayValue = [[charactersArray objectAtIndex:i] intValue];
            y = y + 37;
            UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            characterButton.frame = CGRectMake(y,200.0, 35.0, 35.0);

            [self.wordButtons addObject:characterButton];
            NSString *characterValue = [NSString stringWithFormat:@"%c",[self.jumbledWord characterAtIndex:arrayValue]];
            [characterButton setTitle:characterValue forState:UIControlStateNormal];
            [characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (arrayValue==0)
            {
                arrayValue = 100;
            }
            [characterButton setTag:i+1];
            [self.view addSubview:characterButton];
        }
    }
}

- (void)animation
{
    for(int i=0;i<[self.jumbledWord length];i++)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:i+1];

        CGFloat translation = self.view.frame.size.width - button.frame.size.width ;
        button.transform = CGAffineTransformMakeTranslation(translation, 0);

        [UIView animateWithDuration:0.5f
                              delay:i
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             button.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
        }];
    }
}

@end
于 2013-06-20T11:27:53.660 回答