gpt4 book ai didi

ios - 如何创建一个循环以在每次按下按钮时指定不同的操作?

转载 作者:行者123 更新时间:2023-11-29 12:58:18 24 4
gpt4 key购买 nike

我的 View Controller 中有一个名为“getQuoteButton”的方法,它是一个附加到按钮的 IBAction 方法,每次我点击按钮时,我都会得到一个随机引用。

我为每次展示新报价创建了一个特定的推送动画,我还想创建一个循环,每次我点击按钮时我都可以更改动画,但我不知道如何做。 .

这是我的代码(NOViewController),它是一个单 View 应用程序:

 #import "NOViewController.h"
#include "NOQuotes.h"

@interface NOViewController ()

@end

@implementation NOViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.quotes = [[NOQuotes alloc] init];

UIImage *background = [UIImage imageNamed:@"albert"];

[self.backgroundImageView setImage:background];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

- (IBAction)getQuoteButton
{

self.quotesLabel.text = [self.quotes getRandomQuote];

CATransition *animationTran = [CATransition animation];
[animationTran setType:kCATransitionPush];
[animationTran setDuration:0.7f];
[animationTran setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.quotesLabel.layer addAnimation:animationTran forKey:@"pushAnimation"];

}


@end

非常感谢任何帮助,如果您需要任何其他信息,请告诉我,以便您提供帮助:)

最佳答案

添加一个整数计数器变量,并在每次按下按钮时递增它。将代码添加到检查计数器的 getQuoteButton,并相应地设置动画类型和子类型:

@interface NOViewController () {
int numClicks; // <<=== Added
}

@end

- (void)viewDidLoad
{
[super viewDidLoad];

self.quotes = [[NOQuotes alloc] init];

UIImage *background = [UIImage imageNamed:@"albert"];

[self.backgroundImageView setImage:background];
numClicks = 0; // <<=== Added
}

- (IBAction)getQuoteButton
{

self.quotesLabel.text = [self.quotes getRandomQuote];

CATransition *animationTran = [CATransition animation];
switch (numClicks) { // <<=== Added
case 0:
[animationTran setType:kCATransitionPush];
[animationTran setSubtype:kCATransitionFromRight];
break;
case 1:
[animationTran setType:kCATransitionPush];
[animationTran setSubtype:kCATransitionFromLeft];
break;
case 2:
[animationTran setType:kCATransitionPush];
[animationTran setSubtype:kCATransitionFromTop];
break;
... // And so on;
case 13:
[animationTran setType:kCATransitionFade];
// Fade has no subtype
break;
}
numClicks = (numClick+1) % 13; // <<=== Added
[animationTran setDuration:0.7f];
[animationTran setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.quotesLabel.layer addAnimation:animationTran forKey:@"pushAnimation"];
}

您可以通过创建两个数组或一个类型/子类型对数组来缩短此代码,但是实现此目的需要在点击时递增成员变量的想法。

关于ios - 如何创建一个循环以在每次按下按钮时指定不同的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20453485/

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