gpt4 book ai didi

ios - 如何识别特定图像在 imageview 动画期间正在动画?

转载 作者:行者123 更新时间:2023-11-29 02:41:49 25 4
gpt4 key购买 nike

我正在创建一个计时器,用于播放一首歌曲 6 秒。但我希望每当我单击该按钮时,它都会识别在该特定时刻动画的特定图像。为此,我使用了 CALayer,但它是没有给出图像名称。

这是我的代码:

-(void)playSong
{
timerButton.imageView.animationImages =[NSArray arrayWithObjects:[UIImage imageNamed:@"Timer6.png"],[UIImage imageNamed:@"Timer5.png"],
[UIImage imageNamed:@"Timer4.png"],[UIImage imageNamed:@"Timer3.png"],
[UIImage imageNamed:@"Timer2.png"],[UIImage imageNamed:@"Timer1.png"],
nil];
timerButton.imageView.animationDuration=6.0;
[self.player play];
[timerButton.imageView startAnimating];
}

- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
CALayer *player1 = timerButton.imageView.layer;

}
}

请帮帮我。

最佳答案

很遗憾,您无法从 UIImageView 中获取图像名称或 UIImage对象。

但是,您可以将可访问性标识符 设置为UIImage对象,在您的情况下,可能是它的文件名。
然后简单地做 someUIImageObject.accessibilityIdentifier应该返回文件名。
( refer answer )

例子:

UIImage *image = [UIImage imageNamed:@"someImage.png"];
[image setAccessibilityIdentifier:@"someImage"];

NSLog(@"%@",image.accessibilityIdentifier);

现在...您会期望 timerButton.imageView.image.accessibilityIdentifier给你图像名称,但当 UIImageView 时,这不是它的工作方式正在动画。
这一次,我们需要通过 UIImageView 访问它s animationImages数组属性。
为此,我们需要一些自定义逻辑来获取 UIImage来自这个 animationImages 的对象数组。

我们将首先记录我们开始为图像设置动画的时间,并通过一些基本的数学计算,我们可以计算出 animationImages 的索引。当前显示在 UIImageView 中的数组.
( refer answer )


答案(代码):

-(void)playSong
{
NSMutableArray *arrImages = [[NSMutableArray alloc] init];
for(int i = 6 ; i > 0 ; i--) {
//Generate image file names: "Timer6.png", "Timer5.png", ..., "Timer1.png"
NSString *strImageName = [NSString stringWithFormat:@"Timer%d.png",i];

//create image object
UIImage *image = [UIImage imageNamed:strImageName];
//remember image object's filename
[image setAccessibilityIdentifier:strImageName];

[arrImages addObject:image];
}

[timerButton.imageView setAnimationImages:arrImages];
[timerButton.imageView setAnimationDuration:6.0f];
[timerButton.imageView setAnimationRepeatCount:0];

[self.player play];
[timerButton.imageView startAnimating];

//record start time
startDate = [NSDate date]; //declare "NSDate *startDate;" globally
}

- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
[self checkCurrentImage];
}
}

-(void)checkCurrentImage
{
//logic to determine which image is being displayed from the animationImages array

NSTimeInterval durationElapsed = -1 * [startDate timeIntervalSinceNow];
NSTimeInterval durationPerFrame = timerButton.imageView.animationDuration / timerButton.imageView.animationImages.count;
int currentIndex = (int)(durationElapsed / durationPerFrame) % timerButton.imageView.animationImages.count;

UIImage *imageCurrent = timerButton.imageView.animationImages[currentIndex];
NSString *strCurrentImage = imageCurrent.accessibilityIdentifier;

NSLog(@"%@",strCurrentImage);
}

关于ios - 如何识别特定图像在 imageview 动画期间正在动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678744/

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