旋转木马不起作用

时间:2017-06-29 05:00:21

标签: ios objective-c carousel

在以下实现中,无论我尝试过什么,它都不会在旋转木马上显示图像。我已经设置了委托和数据源。

标题

#import "UIImageView+AFNetworking.h"
#import <AFNetworking/AFNetworking.h>
#import "iCarousel.h"

@interface GalleryViewController : UIViewController<iCarouselDataSource, iCarouselDelegate>
@property (strong, nonatomic) UIImageView *galleryImageView;
@property (nonatomic, strong) NSMutableArray *gElements;
@property (nonatomic, strong) iCarousel *carousel;


@end

实施

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gElements = [[NSMutableArray alloc] init];
    self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    self.carousel.delegate = self;
    self.carousel.dataSource = self;
    self.carousel.type = iCarouselTypeCylinder;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^(void) {
        [self loadGalleryImages];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.carousel reloadData];
        });
    });
}

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [gElements count];
}

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

    self.galleryImageView = nil;
    //create new view if no view is available for recycling
    if (carouselView == nil)
    {
        carouselView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
        self.galleryImageView = [[UIImageView alloc]initWithFrame:carouselView.frame];
        self.galleryImageView.tag = 0;
        [carouselView addSubview:self.galleryImageView];
     }
    else
    {
        self.galleryImageView = (UIImageView*)[carouselView viewWithTag:0];
    }

    if([gElements count] >0)
    {
     // gElement has 10 NSURL objects
       NSURLRequest *request = [NSURLRequest requestWithURL:[gElements objectAtIndex:index]];

        NSLog(@ "index %ld", (long)index);
        __weak typeof(self) weakSelf = self;

        [galleryImageView setImageWithURLRequest:request
                        placeholderImage:nil
                                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                            weakSelf.galleryImageView.image = image;                                         
         }
         failure:nil];
    }
    return carouselView;
}

2 个答案:

答案 0 :(得分:1)

您需要在下载图片时重新加载轮播。

注意:比较并替换您的代码

-(UIView *) carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    UIView  *globleView = (UIView *)view;
    if (!globleView) {
        globleView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth(carousel), 250)];
        globleView.backgroundColor = [UIColor clearColor];
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UtilityClass placeholder];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.tag = 100;
        [globleView addSubview:imageView];

        if (index == 0) {
            [self refreshCarousleViewWithView:globleView withIndex:index];
        }
    }
    return globleView;
}



- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
}

- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index{
    return YES;
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel{
    [self refreshCarousleViewWithView:carousel.currentItemView withIndex:carousel.currentItemIndex]; // 
}

-(void)refreshCarousleViewWithView:(UIView *)currentView withIndex:(NSInteger)index{
    if([gElements count] >0)
    {
     // gElement has 10 NSURL objects
       NSURLRequest *request = [NSURLRequest requestWithURL:[gElements objectAtIndex:index]];

        NSLog(@ "index %ld", (long)index);
      UIImageView *galleryImageView = (UIImageView *)[view viewWithTag:100];    
        [galleryImageView setImageWithURLRequest:request
                        placeholderImage:nil
                                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                            galleryImageView.image = image;                                         
         }
         failure:nil];
    }

}

您忘记添加轮播以供查看。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gElements = [[NSMutableArray alloc] init];
    self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    self.carousel.delegate = self;
    self.carousel.dataSource = self;
    self.carousel.type = iCarouselTypeCylinder;
[self.view addSubView:self.carousel]; // add this line
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^(void) {
        [self loadGalleryImages];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.carousel reloadData];
        });
    });
}

答案 1 :(得分:0)

我不知道你错过了什么,但这是我的代码正在运行。

class FindGigCardViewVC: BaseVC,iCarouselDataSource, iCarouselDelegate {

    @IBOutlet var carousel: iCarousel!
    var arrGigData = NSMutableArray()

    override func viewDidLoad()
    {
         super.viewDidLoad()
         carousel.type = .linear
    }

    func reloadDataInCarousel()
    {
        carousel.reloadData()
    }

    // MARK: - carousel delegate

    func numberOfItems(in carousel: iCarousel) -> Int {
         return arrGigData.count
    }

    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView
    {
         return your view
    }

    func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
         if (option == .spacing) {
              return value * 1.1
         }
         return value
      }

    func carouselCurrentItemIndexDidChange(_ carousel: iCarousel)
    {
         print(carousel.currentItemIndex)
    }
}