gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:35:08 24 4
gpt4 key购买 nike

我有两个 UIButton,我想先单击 Number1 UIButton,然后再单击 Number 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];

}

`

最佳答案

根据我的理解,您的 buttonClicked 操作接收发送者(被点击的 UIButton)并且您旋转相同的。

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

  1. 为这两个按钮创建一个 IBOutlet(不是 IBAction)。在您的 rotateButtons 方法中,不仅要旋转发送器 UIButton,还要旋转这两个按钮。
  2. 按照上面@lnafziger 的建议,在 XIB 中用数字标记两个按钮。在 rotateButtons 方法中旋转这两个按钮。您可以使用以下代码获取每个 UIButton:

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

关于ios - 如何在不单击按钮的情况下开始操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10769857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com