通过从右移动到其原始位置来动画一系列UIButton

时间:2013-06-20 10:33:27

标签: iphone animation uibutton

我对动画一无所知,我曾经使用过很多动画,但根据我的要求没有任何工作。我有一系列根据字长显示的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 - * 关闭帖子 *:我使用以下代码从视图中删除按钮序列,然后带下一个按钮序列,即下一个字

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

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

1 个答案:

答案 0 :(得分:3)

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