![enter image description here](https://istack.dev59.com/DDcP3.webp)
![enter image description here](https://istack.dev59.com/5zoJs.webp)
![enter image description here](https://istack.dev59.com/cA8bw.webp)
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
NSLog(@"size=%d",audio.count);
return audio.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
NSLog(@"get view for index %d",index);
Audio *aud = [audio objectAtIndex:index];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
AudioView *aView;
float height = screenWidth*.5;
float width = height * (16.0/9.0);
//create new view if no view is available for recycling
if (view == nil)
{
aView = [[[NSBundle mainBundle] loadNibNamed:@"AudioView" owner:self options:nil] objectAtIndex:0];
[aView setFrame:CGRectMake(0, 0, width, height)];
}else{
aView=(AudioView *)view;
}
aView.layer.cornerRadius=8;
NSLog(@"%f",aView.frame.size.height);
NSLog(@"%f",aView.frame.size.width);
aView.backgroundColor=[UIColor alizarinColor];
aView.textLabel.text=aud.text;
aView.titleLabel.text=aud.name;
if (rowCurrentlyPlaying==index&&isPlaying) {
aView.imageView.image = [UIImage imageNamed:@"pause.png"];
}else{
aView.imageView.image = [UIImage imageNamed:@"play.png"];
}
return aView;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
if (option == iCarouselOptionSpacing)
{
return value * 1.1f;
}
return value;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
NSLog(@"Clicked");
if(index==self.carousel.currentItemIndex){
Audio *aud = [audio objectAtIndex:index];
NSURL *audUrl = [NSURL URLWithString:aud.audioUrl];
if (rowCurrentlyPlaying==index) {
if (isPlaying) {
[anAudioStreamer pause];
isPlaying=NO;
}else{
[anAudioStreamer play];
isPlaying=YES;
}
}else{
rowCurrentlyPlaying=index;
isPlaying=YES;
aPlayerItem = [[AVPlayerItem alloc] initWithURL:audUrl];
anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
[anAudioStreamer play];
}
[carousel reloadData];
}
}
编辑:我在iPad上也尝试了这个方法,以下截图可能有助于解决问题(请忽略可笑的字体大小)。从未被选中时,看起来它只是没有完全延伸到整个宽度。
正确布局:
不正确的布局: