通过从右移动到其原始位置来动画uibutton序列



我对动画没有任何概念,所以我已经使用了相当多的动画建议,但没有根据我的要求工作。我有一个序列的uibutton显示根据一个字的长度,即。每个按钮包含该单词的一个字符。所以我想做的是,当放置单词本身时,我想让按钮从右边回到它们提到的位置(坐标)。下面是我如何创建等于单词长度的按钮。

-(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];
        }
    }
}

p。S-*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];
        }
    }
}

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

ViewController。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

最新更新