2012-04-20 62 views
0

我有UIViews的數組,我想他們在格子狀的主視圖安排,我也希望它動畫(即電網應該會出現一個一個慢慢)。在for循環中嘗試了動畫,但沒有工作,但想出了一個更接近我想要的東西,但只是想知道是否有更好的方法來做到這一點。我知道CAAnimationGroup(begintime)有一些屬性,但確定如何將它連接到addtosubview。這裏是我的代碼,讓我知道是否有更好的方法來做到這一點。動畫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

+0

它怎麼沒有在循環內工作?你是否嘗試將循環放入動畫塊中,而不是將其包裹起來?這樣你就不會創建多個單一的動畫,而是一個稍微複雜的動畫。 – EmilioPelaez 2012-04-20 02:40:35

+0

如果我試圖把整個循環放在動畫塊中,我不會一個接一個地出現動畫效果,只能一次完成顯示。如果你運行我的代碼,你會知道我想要達到的目標。 – anoop4real 2012-04-20 04:27:19

+0

更新了代碼以添加流暢的動畫,因爲我無法找到更好的方法,所以我將繼續使用此方法。我將等待1-2天,並將其標記爲已回答。 – anoop4real 2012-04-26 01:47:11

回答

0

您好,我也用動畫中的循環,但沒有奏效 正確,然後我將其稱爲動畫視圖的方法,多次 和它的工作正常。我認爲這是簡單應用 動畫的更好方法。通過調用一個方法。我在這裏放一些代碼可能會幫助你。

- (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. 
+0

您好,王子,感謝您的快速回復,但我並非試圖實現動畫多次的圖像。下面就是我要做的,我有UIView的(基本上磚)的陣列,我想將它安排我的屏幕上,我想這樣做與動畫即第二視圖後,第二等第一,第3後出現而不是把它們全部放在屏幕上。請嘗試運行我的代碼,您將知道我正在努力實現的目標。 – anoop4real 2012-04-20 08:23:33