如何识别特定图像是在图像视图动画期间动画?

时间:2014-09-05 04:44:57

标签: ios ios7 uiimageview uibutton calayer

我正在创建一个用于播放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;

 }
}

请帮帮我。

1 个答案:

答案 0 :(得分:1)

很遗憾,您无法从UIImageViewUIImage对象获取图片名称。

但是,您可以为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);
}