IOS 实现图片Carousel效果---Cover Flow效果

Carousel效果也叫Cover Flow效果,就是图片以3D的形式进行左右滑动。

//
//  Carousel.h
//
  
#import <uikit uikit.h="">
  
@interface Carousel : UIView <uiscrollviewdelegate>
{
    UIPageControl *pageControl;
    NSArray *images;
}
  
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSArray *images;
  
- (void)setup;
  
@end
  
//
//  Carousel.m
//
  
#import "Carousel.h"
  
@implementation Carousel
  
@synthesize pageControl;
@synthesize images;
  
#pragma mark - Override images setter
  
- (void)setImages:(NSArray *)newImages
{
    if (newImages != images)
    {
        [newImages retain];
        [images release];
        images = newImages;
  
        [self setup];
    }
}
  
#pragma mark - Carousel setup
  
- (void)setup
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    [scrollView setDelegate:self];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setPagingEnabled:YES];
    [scrollView setBounces:NO];
  
    CGSize scrollViewSize = scrollView.frame.size;
  
    for (NSInteger i = 0; i < [self.images count]; i++)
    {
        CGRect slideRect = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width, scrollViewSize.height);
  
        UIView *slide = [[UIView alloc] initWithFrame:slideRect];
        [slide setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
  
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
        [imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:i]]];
        [slide addSubview:imageView];
        [imageView release];
  
        [scrollView addSubview:slide];
        [slide release];
    }
  
    UIPageControl *tempPageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollViewSize.height - 20, scrollViewSize.width, 20)];
    [self setPageControl:tempPageControll];
    [tempPageControll release];
    [self.pageControl setNumberOfPages:[self.images count]];
    [scrollView setContentSize:CGSizeMake(scrollViewSize.width * [self.images count], scrollViewSize.height)];
  
    [self addSubview:scrollView];
    [scrollView release];
    [self addSubview:self.pageControl];
}
  
#pragma mark - UIScrollViewDelegate
  
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
 [self.pageControl setCurrentPage:page];
}
  
#pragma mark - Cleanup
  
- (void)dealloc
{
    [pageControl release];
    [images release];
    [super dealloc];
}
  
@end
  
//
//  ViewController.m
//  SimpleCarousel
//
  
#import "ViewController.h"
#import "Carousel.h"
  
@implementation ViewController
  
- (void)viewDidLoad
{
    [super viewDidLoad];
  
    // Initialize carousel object with frame size of images
    Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, 320, 205)];
  
    // Add some images to carousel, we are passing autoreleased NSArray
    [carousel setImages:[NSArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", nil]];
  
    // Add carousel to view
    [self.view addSubview:carousel];
  
    // Cleanup
    [carousel release];
}
  
@end</uiscrollviewdelegate></uikit>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值