码迷,mamicode.com
首页 > 其他好文 > 详细

展示图

时间:2014-11-02 09:31:29      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   for   sp   strong   on   cti   ad   

ViewController.h

@interface ViewController : UIViewController<UIScrollViewDelegate>
{
    UIPageControl *_pageCtrl;
    UIScrollView *_scrollView;
}

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

//    NSMutableArray *imgArrary = [[NSMutableArray alloc] init];
    
    //创建滚动视图
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 300)];
    //隐藏滚动条
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    //设置代理方法
    _scrollView.delegate = self;
    
    //分页效果
    _scrollView.pagingEnabled = YES;
    //设置内容尺寸
    _scrollView.contentSize = CGSizeMake(320*5, 300);
    [self.view addSubview:_scrollView];
    
    for(int i=1; i<6; i++) {
    
        NSString *name = [NSString stringWithFormat:@"image%d.jpg",i];
        UIImage *image = [UIImage imageNamed:name];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(320*(i-1), 0, 320, 300);
        [_scrollView addSubview:imageView];
    }
    
    //创建分页控键
    _pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 300, 320, 20)];
    _pageCtrl.numberOfPages = 5;
    _pageCtrl.currentPage = 0;
    [_pageCtrl addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageCtrl];
    
}

#pragma mark - UIScrollView delegate
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//
//    /*
//     0:  0--320 /320  0
//     1;   320--640  /320  1
//     */
//    
//    int count = scrollView.contentOffset.x/320;
//    _pageCtrl.currentPage = count;
//    
//}

//视图停止以后调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int count = scrollView.contentOffset.x/320;
    _pageCtrl.currentPage = count;
}

- (void)pageAction {

    int index = _pageCtrl.currentPage;
    
    CGPoint point = CGPointMake(320*index, 0);
    
    
//    _scrollView.contentOffset = point;
    [_scrollView setContentOffset:point animated:YES];
    
}

@end


展示图

标签:style   io   ar   for   sp   strong   on   cti   ad   

原文地址:http://blog.csdn.net/pengyuan_d/article/details/40682363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!