2012-05-26 173 views
1

我有兩個UIButtons,我想在數字1 UIButton首先單擊,然後在2號UIButton。點擊第二個UIButton後,我想讓兩個UIButton自動旋轉。我用CABasicAnimation來旋轉它們。這兩個UIButton的操作與buttonClicked的操作相同。這兩個按鈕使用不同的CABasicAnimation方法。如何在不點擊按鈕的情況下開始操作?

我已經創建了一個新的方法來旋轉它,但是當我從button clicked動作(對於兩個按鈕相同)調用它時,只有第一個被旋轉。爲了調用旋轉方法,我使用[self rotateButtons];

問題是,如果我插入新的UIButton並給IBAction新按鈕就沒有問題。但我不想在圖層中添加一個新按鈕。

我如何修復rotateButtons以便兩個按鈕可以同時旋轉

下面是代碼:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
    if ([value isEqualToString:@"SecondImage"]) 
    { 
     UIImageView *myView2=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton+100]; 
     [myView2 setHidden:FALSE]; 
     UIImageView *myView=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton]; 
     [myView setHidden:TRUE]; 
     CALayer *layer2 = myView2.layer; 
     layer2.anchorPoint = CGPointMake(0.5f, 0.5f); 
     CABasicAnimation *animateImage2 = [CABasicAnimation animationWithKeyPath: @"transform"]; 
     CATransform3D transform2 = CATransform3DIdentity; 
     transform2.m34 = 1.0/-900.0; 
     self.view.layer.transform = transform2; 
     layer2.transform = CATransform3DMakeRotation(-M_PI/2, 0.0f, 1.0f, 0.0f); 
     self.view.layer.transform = transform2; 
     transform2 = CATransform3DRotate(transform2, -M_PI, 0.0f, 1.0f, 0.0f); 
     animateImage2.repeatCount = 1.0; 
     animateImage2.toValue = [NSValue valueWithCATransform3D: transform2]; 
     animateImage2.duration = 0.5; 
     animateImage2.fillMode = kCAFillModeForwards; 
     animateImage2.removedOnCompletion = NO; 
     [layer2 addAnimation: animateImage2 forKey: @"halfAnimatedImage"]; 

     return; 
    } 
} 

-(void) rotateView1 
{ 
    UIImageView *firstImage=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton]; 
    CALayer *layer = firstImage.layer; 
    layer.anchorPoint = CGPointMake(0.5f, 0.5f); 
    CABasicAnimation *animateImage = [CABasicAnimation animationWithKeyPath: @"transform"]; 
    CATransform3D transform = CATransform3DIdentity; 
    transform.m34 = 1.0/-900.0; 
    self.view.layer.transform = transform; 
    layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f); 
    [animateImage setDelegate:self ]; 
    animateImage.repeatCount = 0.5; 
    animateImage.toValue = [NSValue valueWithCATransform3D: transform]; 
    animateImage.duration = 0.5; 
    animateImage.fillMode = kCAFillModeForwards; 
    animateImage.removedOnCompletion = NO; 
    animateImage.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
    [animateImage setValue:@"SecondImage" forKey:@"halfAnimatedImage"]; 
    [layer addAnimation: animateImage forKey: @"halfAnimatedImage"]; 
} 
-(void) rotateView2 
{ 
    UIImageView *firstImage=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton]; 
    CALayer *layer = firstImage.layer; 
    layer.anchorPoint = CGPointMake(0.5f, 0.5f); 
    CABasicAnimation *animateImage = [CABasicAnimation animationWithKeyPath: @"transform"]; 
    CATransform3D transform = CATransform3DIdentity; 
    transform.m34 = 1.0/-900.0; 
    self.view.layer.transform = transform; 
    layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f); 
    [animateImage setDelegate:self ]; 
    animateImage.repeatCount = 0.5; 
    animateImage.toValue = [NSValue valueWithCATransform3D: transform]; 
    animateImage.duration = 0.5; 
    animateImage.fillMode = kCAFillModeForwards; 
    animateImage.removedOnCompletion = NO; 
    animateImage.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
    [animateImage setValue:@"SecondImage" forKey:@"halfAnimatedImage"]; 
    [layer addAnimation: animateImage forKey: @"halfAnimatedImage"]; 
} 
-(IBAction) buttonRefresh:(UIButton *)sender 
{ 
    numberOfCurrentButton=1; 
    [self rotateView1]; 
    numberOfCurrentButton=2; 
    [self rotateView2]; 
} 
-(IBAction)buttonClicked:(UIButton *)sender 
{ 
    [self buttonRefresh:sender]; 

} 

`

+0

你可能想在你的問題一些代碼來顯示到目前爲止,你已經嘗試過什麼,也許有些圖像以圖形方式描述你」試圖做? –

+0

@MichaelDautermann我上傳程序的代碼。 action buttonClicked用於兩個UIButton。我想,當點擊第二個UIButton時,buttonRefresh動作開始並旋轉兩個按鈕。 – user1114232

+0

由於rotateView1和rotateView2是相同的,那將是更容易地創建一個新的' - (空)rotateViewWithTag:(NSInteger的)標籤{'方法.... – lnafziger

回答

1

按我的理解,你buttonClicked行動收到發件人(UIButton的所單擊),你旋轉一樣。

要旋轉兩種,你可以做以下任一操作:

  1. 兩個按鈕創建一個IBOutlet(不IBAction爲)。在您的rotateButtons方法中,不要只是旋轉發件人UIButton,同時旋轉這兩個按鈕。
  2. 如所建議的通過@lnafziger以上,標籤都與在XIB數字的按鈕。在rotateButtons方法中旋轉這兩個按鈕。您可以通過使用下面的代碼獲取每個的UIButton:

    UIbutton *button1 = (UIButton *)[self.view viewWithTag:101]; 
    
+0

你明白我的ploblem,我認爲你的建議(1號)與IBOutlet中似乎是最好的。我會盡力而且會告訴你。 – user1114232

+0

很高興我能幫到你。它對你有用嗎? – chirag

+0

是的,我使用IBOutlet,它工作。謝謝。 – user1114232

相關問題