阻止 iCarousel 标签滚动

Posted

技术标签:

【中文标题】阻止 iCarousel 标签滚动【英文标题】:Stop iCarousel labels from scrolling 【发布时间】:2015-11-23 10:44:26 【问题描述】:

我所有的标签、完成按钮和后退按钮都在我的 icarousel 中滚动。有没有办法阻止它们随着滑动手势移动,或者它们本质上是固定的?还是将它们一起从函数中取出?

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view

GalleryImage* gi = [self.galleryItem.galleryImages objectAtIndex:index];

UIImageView* imageView = nil;
UILabel* positionLabel = nil;
//UILabel* titleLabel = nil;
UILabel* label = nil;
UIButton* doneButton = nil;
UIView* labelBackground = nil;
UIActivityIndicatorView * spinnerView = nil;
UILabel * errorMessage = nil;
UILabel* titleLabel = nil;

//create new view if no view is available for recycling
if (view == nil) 
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;

    // Image
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
    imageView.contentMode = UIViewContentModeScaleAspectFit;// UIViewContentModeCenter;
    imageView.tag = 1;

    // Spiner
    spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [spinnerView setFrame:imageView.frame];
    spinnerView.tag = 11;

    //Error message
    errorMessage = [[UILabel alloc] initWithFrame:spinnerView.frame];
    [errorMessage setTextAlignment:NSTextAlignmentCenter];
    [errorMessage setTextColor:[UIColor whiteColor]];
    errorMessage.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14.0];
    errorMessage.tag = 111;

    [imageView addSubview:errorMessage];
    [imageView addSubview:spinnerView];
    [view addSubview:imageView];


    // Position
    positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)] ;
    positionLabel.backgroundColor = [UIColor clearColor];
    positionLabel.textAlignment = NSTextAlignmentCenter;
    positionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    positionLabel.numberOfLines=1;
    positionLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14.0];
    positionLabel.textColor = [UIColor whiteColor];
    positionLabel.tag = 2;

    //if(UIInterfaceOrientationPortrait == self.interfaceOrientation)
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    
        // Title
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.carousel.frame.size.width - 20, 100)] ;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        titleLabel.numberOfLines = 4;
        titleLabel.font = [UIFont fontWithName:@"Georgia" size: 18.0];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = [NSString stringWithFormat:@"%@ \n", self.galleryItem.title];
        [view addSubview:titleLabel];

        /*
         *  Manage carousel speed and deceleration rate for portrait mode
         */
        //self.carousel.decelerationRate = 0.175f;
        //self.carousel.scrollSpeed = 0.45f;
    
    else if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    
        /*
         *  Manage carousel speed and deceleration rate for landscape mode
         */
        //self.carousel.decelerationRate = 0.25f;
        //self.carousel.scrollSpeed = 0.55f;
    

    labelBackground = [[UIView alloc] initWithFrame:CGRectMake(0, self.carousel.frame.size.height - 160, self.carousel.frame.size.width , 160)] ;
    CAGradientLayer *labelBackgroundGradient = [CAGradientLayer layer];
    labelBackgroundGradient.frame = labelBackground.bounds;
    labelBackgroundGradient.colors = [NSArray arrayWithObjects: (id)[[UIColor clearColor]CGColor],
                                                                (id)[[UIColor blackColor]CGColor],
                                                                nil
                                      ];
    [labelBackground.layer insertSublayer:labelBackgroundGradient atIndex:0];

    labelBackground.opaque = false;
    labelBackground.tag = 5;

    // Description

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(    0.0f,
                                                                    0.0f,
                                                                    labelBackground.bounds.size.width - 20.0f,
                                                                labelBackground.bounds.size.height)] ;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.numberOfLines = 0;
    label.font = [label.font fontWithSize:12];
    label.textColor = [UIColor whiteColor];
    label.text = gi.descriptionGalleryImage;
    [label autoAdjustGalleryCaptionSize];
    [label setFrame:CGRectMake(     label.bounds.origin.x + 10.0f,
                                    label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
                                    label.bounds.size.width,
                                    label.bounds.size.height
     )];
    label.tag = 6;

    [labelBackground addSubview:label];
    [view addSubview:labelBackground];

 else 
    imageView = (UIImageView *)[view viewWithTag:1];
    spinnerView = (UIActivityIndicatorView*)[imageView viewWithTag:11];
    errorMessage = (UILabel*) [imageView viewWithTag:111];
    positionLabel = (UILabel *)[view viewWithTag:2];
    doneButton = (UIButton *)[view viewWithTag:3];
    //titleLabel = (UILabel *)[view viewWithTag:4];
    labelBackground = (UIView *)[view viewWithTag:5];
    label = (UILabel*)[view viewWithTag:6];

//Start image loading
ImageSize* imSize = [gi getLargestImage];
[spinnerView startAnimating];
errorMessage.text = @"";

[imageView sd_setImageWithURL:[NSURL URLWithString:imSize.source] placeholderImage:nil options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize) 
    //progress bar could be here

 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) 
    //remove progressbar
    [spinnerView stopAnimating];

    //Display error message
    if(error)
    
        //errorMessage.text = error.localizedDescription;
        errorMessage.text = @"Cannot load image";
    

    //Assign image
    if(image)
    
        [imageView setImage:image];
    
];

positionLabel.text = [NSString stringWithFormat:@"%d of %ld", (index + 1), (long)[self.galleryItem.galleryImages count]];



UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)];
v.backgroundColor = [UIColor blackColor];
v.alpha = 0.6;

[view addSubview:v];
[view addSubview:positionLabel];


// Done button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 0 , 50, 30);
[doneButton setImage:[UIImage imageNamed:@"back_button_white"] forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0,5,0,5);
[doneButton setTintColor:[UIColor whiteColor]];
[doneButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:doneButton];

/*
 *  The description. Why are we setting the frame for the label multiple times?
 *  We need to do this so it can autoadjust its size and position according 
 *  to the text contained within it.
 */
[label setText:gi.descriptionGalleryImage];
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake(     label.bounds.origin.x + 10.0f,
                                label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
                                label.bounds.size.width,
                                label.bounds.size.height
                           )];



labelBackground.hidden = self.hideLandscapeDetails;
label.hidden = self.hideLandscapeDetails;
doneButton.hidden = self.hideLandscapeDetails;
positionLabel.hidden = self.hideLandscapeDetails;
v.hidden =self.hideLandscapeDetails;
titleLabel.hidden = self.hideLandscapeDetails;

self.tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHideInfo:)];
[view addGestureRecognizer:self.tap1];

// scroll portrait view to same index
if (self.delegate) 
    [self.delegate setReturnIndex:self.carousel.currentItemIndex];


id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Image Gallery"     // Event category (required)
                                                      action:@"Gallery Image Viewed"  // Event action (required)
                                                       label:@"Picture swipe"          // Event label
                                                       value:[NSNumber numberWithLong:(index + 1)]] build]];    // Event value
return view;

【问题讨论】:

【参考方案1】:

标签应位于 iCarousel 上方以执行您想要的操作,而不是在轮播视图内。

【讨论】:

您是否认为有一种更简单的方法可以以编程方式表示不要滚动到这些元素,而是通过它们并将它们取出?有点老套,但可能会节省我一些时间,因为这个版本很快就会被废弃! 放置在 viewForItemAtIndex 中的任何内容都会滚动,这就是标签必须独立于轮播的原因。

以上是关于阻止 iCarousel 标签滚动的主要内容,如果未能解决你的问题,请参考以下文章

如何获取 iCarousel 当前可见视图?

Swift - 使用 UiView 的 iCarousal

暂时阻止 iCarousel 滚动

iPad 上的 iCarousel 性能问题

iCarousel 滚动不顺畅

在 Swift 的 iCarousel 中预览 UIWebView