UIImageView中用計時器實現5張圖片不斷變換。能不能獲取當前位於uiimage的圖片名稱?查詢了一些方法,設置標簽等等,寫了如下代碼:
[_imgView setImage:[UIImage imageNamed:@"tops.png"]];
imgArray=[[NSArray alloc] initWithObjects:@"tops.png",@"position_conv.png",@"stocks.png",@"home.png",@"report.png",nil];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(change) userInfo:nil repeats:YES];
-(void)change
{
int randNum = rand() % (4 - 0) + 0;
[_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
}
使用圖片屬性的可訪問標示符Accessibility identifier,在你的方法中設置圖片 accessibility identifier,你就可以隨時隨地獲取圖片了
-(void)change
{
int randNum = rand() % (4 - 0) + 0;
[_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
[_imgView.image setAccessibilityIdentifier:[imgArray objectAtIndex:randNum]];
}
- (IBAction)currentImgName:(id)sender {
NSLog(@"image name is %@", [_imgView.image accessibilityIdentifier]);
}