Detect which image was clicked in UIImageView with animationImages

前端 未结 4 1757
耶瑟儿~
耶瑟儿~ 2020-12-20 08:50

We can specify images array for a UIImageview, and it will animate the images very nicely. I have subclassed the UIImageView class.

Now whe

4条回答
  •  無奈伤痛
    2020-12-20 09:14

    I used the following workaround... Instead of using the built-in feature of animationImages, I animated the images with custom NSTimer

    - (void)setBannerImagesArray:(NSArray *)bannerImagesArray
    {
        _bannerImagesArray = bannerImagesArray;
        [self.timer invalidate];
        self.currentImageIndex = 0;
        self.image = [self.bannerImagesArray objectAtIndex:self.currentImageIndex];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(displayNextImage)
                                                    userInfo:nil
                                                     repeats:YES];
    
    }
    
    - (void)setup
    {
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked)];
        [self addGestureRecognizer:tapGesture];
    
        self.bannerImagesArray = nil;
    }
    
    - (void)imageClicked
    {
        NSLog(@"Image clicked index: %d", self.currentImageIndex);    
    }
    
    - (void)displayNextImage
    {
        self.currentImageIndex = (self.currentImageIndex + 1) % self.bannerImagesArray.count;
        NSLog(@"Current Image Index %d", self.currentImageIndex);
        self.image = [self.bannerImagesArray objectAtIndex:self.currentImageIndex];
    }
    

提交回复
热议问题