0

我有一个 UIViews 数组,我想以网格形式将它们排列在主视图中,并且我还希望它具有动画效果(即网格应该一个一个缓慢地出现)。尝试在 for 循环中制作动画,但没有奏效,但想出了一个变通方法来实现更接近我想要的东西,但只是想知道是否有更好的方法来做到这一点。我知道 CAAnimationGroup (begintime) 有一些属性,但确定如何将它连接到 addtosubview。这是我的代码,如果有更好的方法,请告诉我。

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGFloat x = 0;
    CGFloat delay = .3;

    for(int i=0;i<30;i++)
    {
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(x, 0, 50, 50)];
        myView.backgroundColor = [UIColor greenColor];
        myView.alpha = 0.0;
        x = x + 50+ 2;
        [UIView animateWithDuration:0.25 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [self.view addSubview:myView];
            myView.transform = CGAffineTransformMakeScale(0.5, 0.5);
           // myView.transform = CGAffineTransformMakeRotation(90);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:.5 animations:^{
                myView.transform = CGAffineTransformMakeScale(1.0, 1.0);
                myView.alpha = 1.0;
            }];

        }];
        delay = delay + .3;
    }
    // Do any additional setup after loading the view, typically from a nib.
}

-anoop

4

1 回答 1

0

嗨,我也在 for 循环中使用了动画,但它不能正常工作,然后我调用一个动画视图的方法,多次并且它工作正常。我认为这是简单应用动画的更好方法。通过调用一个方法。我在这里放一些代码可能会对你有所帮助。

- (void)viewDidLoad
{
    radius=82.097;
    rotationMultiplier=1;
    toAngle = fromAngle = 0;
    toStart = NO;
    center=CGPointMake(161, 233);

    NSString *path = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"mp3"];

    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    [self.player prepareToPlay];

    SingleTouchGesture *wheelRecogniser=[[SingleTouchGesture alloc] initWithTarget:self action:@selector(rotateWheel:)];
    [self.view addGestureRecognizer:wheelRecogniser];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void) rotate:(int) time
{
    CABasicAnimation *topAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    topAnimation.duration=time;
    topAnimation.fromValue= [NSNumber numberWithFloat:fromAngle];
    topAnimation.toValue=[NSNumber numberWithFloat:toAngle];
    topAnimation.delegate = self;
    topAnimation.fillMode = kCAFillModeForwards;
    topAnimation.removedOnCompletion = NO;
    [wheelImage.layer addAnimation:topAnimation forKey:@"transform"];
    fromAngle = toAngle;
}


-(void) rotateWheel:(SingleTouchGesture*) recogniser
{
    toAngle += recogniser.rotation;

    if (toStart) 
    {
        if (recogniser.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Speed === %f",recogniser.speed);
            rotationMultiplier=[self getMultiplier:recogniser.speed];
            NSLog(@"Rotation Multiplier..%f",rotationMultiplier);
            toStart = NO;   
            NSLog(@"State Ended");
            self.player.currentTime=0;
            [self.player play];

            if (recogniser.rotation>=0)
            {
                toAngle =fromAngle+(rotationMultiplier*3.14);
            }
            else
            {
                toAngle =fromAngle-(rotationMultiplier*3.14);
            }
        }

        [self rotate:1];
    }
    else
     {
        if (recogniser.state==UIGestureRecognizerStateChanged) {
            NSLog(@"continue...");
            CGPoint pt=[recogniser locationInView:self.view];

            float dis=[recogniser distanceBetweenPoints:center with:pt];

            if (dis<radius) 
            {
                if (recogniser.rotation>=0) 
                {
                    NSLog(@"Rotation value: +ve: %f",recogniser.rotation);
                    toStart = YES;
                    toAngle =fromAngle+(rotationMultiplier*3.14);         
                    recogniser.rotation = 0; 
                }
                else
                {
                    NSLog(@"Rotation value: -ve: %f",recogniser.rotation);
                    toStart = YES;
                    toAngle=fromAngle-(rotationMultiplier*3.14);              
                    recogniser.rotation = 0;
                }

            }
        }
    }
}

Here rotate method is calling multiple times.
于 2012-04-20T04:38:21.393 回答