用UIScrollView實現相冊,並設置滾動條消失,改變UIPageControl的顏色,實現循環滾動相冊

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController<UIScrollViewDelegate>
{
    UIScrollView * _scroll;
    UIPageControl * _page;
}

@property (nonatomic, copy) NSArray * arr;

@end

- (void)dealloc
{
    [_arr release];
    _arr = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.arr = [NSArray arrayWithObjects:@"10.jpg",@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"1.jpg", nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 400)];
    //設置背景顏色
    [_scroll setBackgroundColor:[UIColor blueColor]];
    //設置大小
    [_scroll setContentSize:CGSizeMake(320 * 12, 400)];
    //設置偏移量
    [_scroll setContentOffset:CGPointMake(320, 0)];
    [_scroll setBounces:NO];
  
    //設置代理人
    _scroll.delegate = self;
    //每次翻滾一頁
    _scroll.pagingEnabled = YES;
    //設置下面滾動條出現和隱藏
    _scroll.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:_scroll];
    [_scroll release];
    
    
    _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 440, 320, 30)];
    [_page setBackgroundColor:[UIColor magentaColor]];
    _page.alpha = 0.5;
    [_page setNumberOfPages:10];
    [_page setCurrentPageIndicatorTintColor:[UIColor blueColor]];
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
    [self.view addSubview:_page];
    [_page release];
    
    int i = 0;
    for (NSString * name in self.arr) {
        
        if (!name) {
            break;
        }
        
        NSLog(@"%@",name);
        UIScrollView * Lscroll = [[UIScrollView alloc]initWithFrame:CGRectMake(320 * i, 0, 320, 400)];
//        NSString * name = [NSString stringWithFormat:@"%d.jpg",i];
        UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
        [image setFrame:CGRectMake(0, 0, 320, 400)];
        //設置放大縮小的值
        [Lscroll setMinimumZoomScale:0.5f];
        [Lscroll setMaximumZoomScale:2.0f];
        
        
        [Lscroll addSubview:image];
        [_scroll addSubview:Lscroll];
        
        Lscroll.delegate = self;
        
        [image release];
        [Lscroll release];
       
        
        i++;
        
    }
    NSLog(@"%d",i);
//    UIImageView * image = [[UIImageView alloc]init];
    
}

#pragma mark -
#pragma mark pageControl

- (void)pageAction:(id)sender
{
    UIPageControl * page = (UIPageControl *)sender;
    
    [_scroll setContentOffset:CGPointMake(page.currentPage * 320 + 320, 0)];
    
    
}

#pragma mark -
#pragma mark scroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    [_page setCurrentPage:_scroll.contentOffset.x/300];
    
    if (_scroll.contentOffset.x == 320 * 11) {
        [_scroll setContentOffset:CGPointMake(320, 0)];
        _page.currentPage = 0;
    }else if (_scroll.contentOffset.x == 0){
        [_scroll setContentOffset:CGPointMake(320 * 10, 0)];
        _page.currentPage = 10;
    }else{
        [_page setCurrentPage:(_scroll.contentOffset.x-320)/320];
    }
    
}



- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSArray * arr = [scrollView subviews];
    
    return [arr objectAtIndex:0];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
//    UIScrollView * scro = [[_scroll subviews] objectAtIndex:_page.currentPage];
//    [scro setZoomScale:1.0f];
    
    for (UIScrollView * temp in [scrollView subviews]) {
        if ([temp isKindOfClass:[UIScrollView class]]) {
            [temp setZoomScale:1];
        }
    }

 }



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章