转型无法执行两次

时间:2012-09-10 02:06:42

标签: objective-c ios5 transformation

我有这个功能,

-(void)transitionstar{
star.hidden = NO;
star2.hidden = NO;
star3.hidden = NO;
star4.hidden = NO;
star5.hidden = NO;
star6.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4f];
[UIView animateWithDuration:0.0 animations:^{
    CGAffineTransform scale = CGAffineTransformMakeScale(1, 1);
    CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
     CGAffineTransform rotate2 = CGAffineTransformMakeRotation(-360.0);
     CGAffineTransform rotate3 = CGAffineTransformMakeRotation(-720.0);
     CGAffineTransform rotate4 = CGAffineTransformMakeRotation(720.0);
     CGAffineTransform rotate5 = CGAffineTransformMakeRotation(1080.0);
     CGAffineTransform rotate6 = CGAffineTransformMakeRotation(-1080.0);
    CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
     CGAffineTransform translate2 = CGAffineTransformMakeTranslation(600, -600);
     CGAffineTransform translate3 = CGAffineTransformMakeTranslation(400, 400);
     CGAffineTransform translate4 = CGAffineTransformMakeTranslation(-200, 200);
     CGAffineTransform translate5 = CGAffineTransformMakeTranslation(900, -300);
     CGAffineTransform translate6 = CGAffineTransformMakeTranslation(-200, 500);

    CGAffineTransform transform =  CGAffineTransformConcat(translate, scale);
    transform = CGAffineTransformConcat(transform, rotate);

    CGAffineTransform transform2 =  CGAffineTransformConcat(translate2, scale);
    transform2 = CGAffineTransformConcat(transform2, rotate2);

    CGAffineTransform transform3 =  CGAffineTransformConcat(translate3, scale);
    transform3 = CGAffineTransformConcat(transform3, rotate3);

    CGAffineTransform transform4 =  CGAffineTransformConcat(translate4, scale);
    transform4 = CGAffineTransformConcat(transform4, rotate4);
    CGAffineTransform transform5 =  CGAffineTransformConcat(translate5, scale);
    transform5 = CGAffineTransformConcat(transform5, rotate5);
    CGAffineTransform transform6 =  CGAffineTransformConcat(translate6, scale);
    transform6 = CGAffineTransformConcat(transform6, rotate6);

    star.transform = transform;
    star2.transform = transform2;
    star3.transform = transform3;
    star4.transform = transform4;
    star5.transform = transform5;
    star6.transform = transform6;
}
                 completion:^(BOOL finished){
                     if (finished) {
                         star.hidden = YES;
                         star2.hidden = YES;
                         star3.hidden = YES;
                         star4.hidden = YES;
                         star5.hidden = YES;
                         star6.hidden = YES;
                     }
                 }];
[UIView commitAnimations];
}

当我第一次打电话时,确实有效。

然而,我再次在相同的视图中调用,它无法执行并卡在那里。

- 隐藏的工作

- 过渡不起作用

- 旋转不起作用

-nslog work

为什么第二次会变成这样?

更新

if(!positionrepeat)
        {
            //Display Correct IMAGE;
            [isrepeat addObject:[NSNumber numberWithInt:positionvalue]];
            //soundeffect = [self createSoundID: @"correct"];
            //AudioServicesPlaySystemSound(soundeffect);
            [self displayresulttext:@"correct"];
            [self.view setNeedsDisplay];
            [self transitionstar];
            correct++;
            completed.text = [NSString stringWithFormat:@"%d", correct];
            [self result];
        }

这就是我调用函数的方式。但是,它仍然相同。

2 个答案:

答案 0 :(得分:0)

我假设您在上面编写的方法在从UIView派生的类中工作。

如果这个假设是正确的,那么在必须重新绘制视图时可以调用您的方法。

要重绘视图,您必须调用setNeedsDisplay方法,如下所示:

[myCustomView setNeedsDisplay];

答案 1 :(得分:0)

在这段代码中你改变了转换

  star.transform = transform;
        star2.transform = transform2;
        star3.transform = transform3;
        star4.transform = transform4;
        star5.transform = transform5;
        star6.transform = transform6;

例如,考虑你的star1初始变换是x, 现在你正在为变换做一些计算......

CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform transform =  CGAffineTransformConcat(translate, scale);
    transform = CGAffineTransformConcat(transform, rotate);

然后

您将星号x的变换更改为y ...

star.transform = transform;//say this is y

现在你的星变换会变得很好......

再次通过调用再次执行此代码,所以你的初始星变换是y,你的等同变换为y所以没有必要,你没有看到任何结果......

y = y所以没有结果......

所以需要改变一些像这样或某些你想要的逻辑,但这是一个想法......

第一个更改:您需要存储每个星的变换,因此您需要一些变换类型的变量...

  CGAffineTransform star1Initial,star2Initial,star3Initial,star4Initial,star5Initial,star6Initial;

第二次更改:您需要在视图中保存星标的初始变换吗?

    star1Initial=star1.transform;
    star2Initial=star2.transform;
    star3Initial=star3.transform;
    star4Initial=star4.transform;
    star5Initial=star5.transform;
    star6Initial=star6.transform;


-(void)transitionstar{
    star1.hidden = NO;
    star2.hidden = NO;
    star3.hidden = NO;
    star4.hidden = NO;
    star5.hidden = NO;
    star6.hidden = NO;

        star1.transform=star1Initial;
        star2.transform=star2Initial;
        star3.transform=star3Initial;
        star4.transform=star4Initial; 
        star5.transform=star5Initial;
        star6.transform=star6Initial;


//    [UIView beginAnimations:nil context:NULL];
//    [UIView setAnimationDuration:0.4f];
    [UIView animateWithDuration:1.0 animations:^{
        CGAffineTransform scale = CGAffineTransformMakeScale(1, 1);
        CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
        CGAffineTransform rotate2 = CGAffineTransformMakeRotation(-360.0);
        CGAffineTransform rotate3 = CGAffineTransformMakeRotation(-720.0);
        CGAffineTransform rotate4 = CGAffineTransformMakeRotation(720.0);
        CGAffineTransform rotate5 = CGAffineTransformMakeRotation(1080.0);
        CGAffineTransform rotate6 = CGAffineTransformMakeRotation(-1080.0);
        CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
        CGAffineTransform translate2 = CGAffineTransformMakeTranslation(600, -600);
        CGAffineTransform translate3 = CGAffineTransformMakeTranslation(400, 400);
        CGAffineTransform translate4 = CGAffineTransformMakeTranslation(-200, 200);
        CGAffineTransform translate5 = CGAffineTransformMakeTranslation(900, -300);
        CGAffineTransform translate6 = CGAffineTransformMakeTranslation(-200, 500);

        CGAffineTransform transform =  CGAffineTransformConcat(translate, scale);
        transform = CGAffineTransformConcat(transform, rotate);

        CGAffineTransform transform2 =  CGAffineTransformConcat(translate2, scale);
        transform2 = CGAffineTransformConcat(transform2, rotate2);

        CGAffineTransform transform3 =  CGAffineTransformConcat(translate3, scale);
        transform3 = CGAffineTransformConcat(transform3, rotate3);

        CGAffineTransform transform4 =  CGAffineTransformConcat(translate4, scale);
        transform4 = CGAffineTransformConcat(transform4, rotate4);
        CGAffineTransform transform5 =  CGAffineTransformConcat(translate5, scale);
        transform5 = CGAffineTransformConcat(transform5, rotate5);
        CGAffineTransform transform6 =  CGAffineTransformConcat(translate6, scale);
        transform6 = CGAffineTransformConcat(transform6, rotate6);

        star1.transform = transform;
        star2.transform = transform2;
        star3.transform = transform3;
        star4.transform = transform4;
        star5.transform = transform5;
        star6.transform = transform6;
    }
                     completion:^(BOOL finished){
                         if (finished) {
                             star1.hidden = YES;
                             star2.hidden = YES;
                             star3.hidden = YES;
                             star4.hidden = YES;
                             star5.hidden = YES;
                             star6.hidden = YES;
                         }
                     }];
//    [UIView commitAnimations];
}

我希望这会对你有帮助....

嗨试试旋转动画的代码.....

- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
    CABasicAnimation* rotationAnimation;
    int rotations=1;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];