Reputation: 120
I am creating a timer which is used to play a song for 6 seconds.But i want that whenever i clicked the button it will recognized the particular image which is animating at that particular instant.For that i used CALayer but it is not giving the image name.
This is my code:
-(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;
}
}
Please help me.
Upvotes: 0
Views: 181
Reputation: 20274
Unfortunately, you cannot get the image name from a UIImageView
or UIImage
object.
You could, however, set an accessibility identifier to a UIImage
object that, in your case, could be it's file name.
Then simply doing someUIImageObject.accessibilityIdentifier
should return the file name.
(refer answer)
Example:
UIImage *image = [UIImage imageNamed:@"someImage.png"];
[image setAccessibilityIdentifier:@"someImage"];
NSLog(@"%@",image.accessibilityIdentifier);
Now... you'd expect timerButton.imageView.image.accessibilityIdentifier
to give you the image name but that's not how it works when the UIImageView
is animating.
This time, we need to access it via the UIImageView
s animationImages
array property.
For this, we will need some custom logic to get the UIImage
object from this animationImages
array.
We'll first record the time we started animating the images and with some basic maths, we can calculate the index of the animationImages
array that is currently being displayed in the 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);
}
Upvotes: 1