如何在滚动时使中心图像更大?

时间:2017-02-23 11:53:51

标签: ios swift

enter image description here

当我水平滚动集合视图时,中心图像应该比其他图像大。怎么实现呢?

2 个答案:

答案 0 :(得分:2)

首先为您的轮播视图类型

提供“自定义”类型
  vwCarousel.type = .custom

然后,通常在轮播中创建您的视图:查看项目方法,如下所示,下面的框架是我需要的框架,您可以根据视图设置所需的框架,

 func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {  
var itemView = UIImageView()
itemView = UIImageView(frame: CGRect(x: 30, y: 0, width: 
carousel.frame.size.width - 60, height: carousel.frame.size.height))
itemView.image = self.arrAboutPictures[index]
itemView.contentMode = .scaleToFill
itemView.setCornerRadius(clipToBounds: true, radius: 10)

  return itemView
}

现在,主要是Coming,将CATranform动画发送到旋转木马,它有自己的方法如下,你可以根据你的要求设置你的“Z”

func carousel(_ carousel: iCarousel, itemTransformForOffset offset: CGFloat, baseTransform transform: CATransform3D) -> CATransform3D {

    let spacing : CGFloat = 0.15
    let distance : CGFloat = 100
    let clampedOffset = min(1.0, max(-1.0, offset))

    var offset = offset

    let z : CGFloat = CGFloat(-fabs(clampedOffset)) * distance

    offset += clampedOffset*spacing
    return CATransform3DTranslate(transform, offset * carousel.itemWidth , 0.5 , z );

}

就是这样,你的自定义动画被创建了。 cheeres .... enter image description here

答案 1 :(得分:1)

您可以使用icarousel视图。

@property (strong, nonatomic) IBOutlet iCarousel *viewICarousel;

使用此方法:

- (void)viewDidLoad
{
    _viewICarousel.dataSource = self;
    _viewICarousel.delegate = self;
    _viewICarousel.pagingEnabled = YES;
    _viewICarousel.type = iCarouselTypeLinear; //user different type

    [_viewICarousel reloadData];

    }
    - (CGFloat)carouselItemWidth:(iCarousel *)carousel
    {
        return (self.view.frame.size.width * 248 / 320);  //Method use for width set of view
    }
    - (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
    {
        if (option == iCarouselOptionSpacing)
        {
            return value * 1.164; // Space between view
        }
        return value;
    }

    - (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
    {
        return  _arrCampaignData.count;
    }
    - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view
    {
        UILabel *label = nil;
        UIImageView *imgView = nil;

        if (view == nil)
        {
            view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width * 278.0 / 320), _viewICarousel.frame.size.height)];
            view.contentMode = UIViewContentModeCenter;
            view.backgroundColor = [UIColor clearColor];

            CGRect rectImage = view.bounds;
            imgView = [[UIImageView alloc]initWithFrame:rectImage];
            [view addSubview:imgView];
        }
        else
        {
            label = (UILabel *)[view viewWithTag:1];
        }

        imgView.backgroundColor = [UIColor colorWithRed:41/255.0 green:171/255.0 blue:135/255.0 alpha:0.3];
        label.text = [NSString stringWithFormat:@"%@",_arrCampaignData[index][KEY_COMPAIGN_NAME]];
        NSString *strURL = [NSString stringWithFormat:@"%@",_arrCampaignData[index][KEY_CAMPAIGN_BANNER]];
        [imgView sd_setImageWithURL:[NSURL URLWithString:strURL]];

        return view;
    }
    - (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
    {
       //select view
    }

希望这对你有帮助。