【问题标题】:How to recognize particular image is animating during imageview animation?如何在 imageview 动画期间识别特定图像正在动画?
【发布时间】:2014-10-29 23:40:02
【问题描述】:

我正在创建一个计时器,用于播放一首歌曲 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;

 }
}

请帮帮我。

【问题讨论】:

    标签: ios ios7 uiimageview uibutton calayer


    【解决方案1】:

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

    但是,您可以将 可访问性标识符 设置为 UIImage 对象,在您的情况下,它可能是它的文件名。
    然后只需执行 someUIImageObject.accessibilityIdentifier 即可返回文件名。
    (refer answer)

    例子:

    UIImage *image = [UIImage imageNamed:@"someImage.png"];
    [image setAccessibilityIdentifier:@"someImage"];
    
    NSLog(@"%@",image.accessibilityIdentifier);
    

    现在...您希望 timerButton.imageView.image.accessibilityIdentifier 为您提供图像名称,但当 UIImageView 进行动画处理时,它不是这样工作的。
    这一次,我们需要通过UIImageViews animationImages 数组属性来访问它。
    为此,我们需要一些自定义逻辑来从 animationImages 数组中获取 UIImage 对象。

    我们将首先记录我们开始为图像设置动画的时间,并通过一些基本的数学运算,我们可以计算出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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      相关资源
      最近更新 更多