如何在不点击按钮的情况下开始操作?

时间:2012-05-26 21:17:30

标签: ios ios5 uikit core-animation

我有两个UIButton,我想先点击Number1 UIButton,然后点击Number UIButton。点击第二个UIButton后,我希望自动轮换两个UIButton。我使用CABasicAnimation来旋转它们。这两个UIButton具有相同的buttonClicked操作。这两个按钮使用不同的CABasicAnimation方法。

我创建了一个新方法以便旋转它,但是当我从button clicked动作调用它时(两个按钮相同)只旋转第一个。为了调用rotate方法,我使用[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];

}

`

1 个答案:

答案 0 :(得分:1)

根据我的理解,您的buttonClicked操作接收发件人(已点击的UIButton)并旋转相同的内容。

要旋转两者,您可以执行以下任一操作:

  1. 为这两个按钮创建一个IBOutlet(而不是IBAction)。在您的rotateButtons方法中,不要只旋转发件人UIButton,而是旋转这两个按钮。
  2. 正如@lnafziger上面所建议的那样,在XIB中标记带有数字的按钮。在rotateButtons方法中,旋转此按钮。您可以使用以下代码获取每个UIButton:

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