iOS:iCarousel中未选择视图的约束

时间:2013-12-20 14:30:48

标签: ios iphone uiview interface-builder icarousel

我正在使用iCarousel来显示我在IB中创建的自定义视图的实例。加载轮播时,轮播中的第一个视图会正确显示,如下所示:

enter image description here

但是,其他索引似乎忽略了我的约束,如下所示: enter image description here

点击此视图后,布局会自行更正。另请注意,第一个索引现在有一个搞砸的布局: enter image description here

知道如何纠正这个问题吗?我的旋转木马代码:

- (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上试过这个,我认为这些截图可能会有所帮助(请忽略热闹的字体大小)。看起来它没有在未选中时拉伸到整个宽度。

正确布局: enter image description here

布局不正确: enter image description here

5 个答案:

答案 0 :(得分:4)

我整天都在寻找这个问题的答案 - 同样的事情发生在我身上。设置Custom UIView的框架不起作用。

解决方案有点奇怪:

  1. 转到自定义UIView的xib文件。我也会假设你 拥有自定义UIVIew
  2. 的相应子类(.h和.m文件)
  3. 添加涵盖整个视图的子视图。我将其称为背景视图。
  4. 将文本视图等添加为背景视图的子视图,并设置相对于背景视图的约束。
  5. 对于背景视图,添加以下约束:超级视图的顶部空间,超级视图的前导空间,宽度等于: ,高度等于:
  6. 创建两个出口,一个来自宽度约束,一个来自步骤4中的高度约束。这些出口应该在 Custom UIView的.h文件。
  7. 在上面的viewForItemAtIndex方法中,继续使用loadNibNamed分配和初始化自定义视图 方法。但是,之后,设置宽度和高度约束 基于旋转木马边界的常数(或任何宽度和宽度) 你想要的高度)。例如,

    CustomView *customView = (CustomView *) view;
    customView.widthConstraint.constant = carousel.bounds.size.width;
    customView.heightConstraint.constant = carousel.bounds.size.height;
    

答案 1 :(得分:1)

我有一段时间没有看过iCarousel,但是这是来自我在这里的任何版本的readMe文件

iCarousel supports the following built-in display types:

- iCarouselTypeLinear
- iCarouselTypeRotary
- iCarouselTypeInvertedRotary
- iCarouselTypeCylinder
- iCarouselTypeInvertedCylinder
- iCarouselTypeWheel
- iCarouselTypeInvertedWheel
- iCarouselTypeCoverFlow
- iCarouselTypeCoverFlow2
- iCarouselTypeTimeMachine
- iCarouselTypeInvertedTimeMachine

You can also implement your own bespoke carousel styles using `iCarouselTypeCustom` and the `carousel:itemTransformForOffset:baseTransform:` delegate method.

NOTE: The difference between `iCarouselTypeCoverFlow` and `iCarouselTypeCoverFlow2` types is quite subtle, however the logic for `iCarouselTypeCoverFlow2` is substantially more complex. If you flick the carousel they are basically identical, but if you drag the carousel slowly with your finger the difference should be apparent. `iCarouselTypeCoverFlow2` is designed to simulate the standard Apple CoverFlow effect as closely as possible and may change subtly in future in the interests of that goal.

我不确定您是否理解iCourousel正在将转换应用于那些不是当前选择的视图,这是故意的。它试图模仿Apple的封面流程api(我们无法使用)..

好的,所以你的carousel对象有一个iCarouselType类型的属性叫做type,从上面的enum中,最好的办法就是尝试每一个找到适合你的那个。

答案 2 :(得分:0)

我发现了一个与iCarousel通常实现不同的东西。

方法- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view

我猜您需要使用:objectAtIndex:index];而不是objectAtIndex:0];

if (view == nil)
{
    aView = [[[NSBundle mainBundle] loadNibNamed:@"AudioView" owner:self options:nil] objectAtIndex:0];
    [aView setFrame:CGRectMake(0, 0, width, height)];
}

我在我的实时应用中使用了一段简单的代码:

- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
if (view == nil)
{
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    view.contentMode = UIViewContentModeCenter;
    [view addSubview:[items objectAtIndex:index]];
}
else
{
    view = [items objectAtIndex:index];
}

view = [items objectAtIndex:index];

return view;
}

希望有所帮助。

答案 3 :(得分:0)

在“viewForItemAtIndex”中创建音频视图时,只需设置视图

[aView setFrame:CGRectMake(0, 0, width, height)];

我认为你必须设置aView的titlelabel和textlabel框架,以获得全宽文本。

对于第二个问题,当您单击视图时,该视图会正确显示但前一个视图的文本会被删除。

我认为您正在重新加载所有轮播视图,点击

[carousel reloadData];

而不是你必须只重新加载你被点击的视图。为此你可以使用

  • (void)reloadItemAtIndex :( NSInteger)索引动画:(BOOL)动画;

这条线可以解决你的问题。

享受....

答案 4 :(得分:0)

对我来说有帮助:

使用iCarousel配置(dataSource等)将功能移至viewDidAppear功能。否则,我的帧不正确。

然后写下这样的东西:

currentView?.frame.size.width  = carouselView.bounds.size.width
currentView?.frame.size.height = carouselView.bounds.size.height

希望对某人有帮助