为多个子视图设置动画addtosubview

时间:2012-04-20 02:33:31

标签: iphone core-animation

我有一个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

1 个答案:

答案 0 :(得分: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.