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

scrollview 例子2

时间:2014-09-21 18:22:30      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   sp   log   

代码:

#import "RootViewController.h"

@implementation RootViewController
@synthesize scrollView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    int pageCount = 3;
    scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    [self.view addSubview:scrollView];
    
    scrollView.backgroundColor = [UIColor redColor];
    scrollView.scrollEnabled = NO;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * pageCount, scrollView.frame.size.height);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.delegate = self;
    
    CGRect rect = [[UIScreen mainScreen]bounds];
    NSArray *colors = [NSArray arrayWithObjects:[UIColor yellowColor], [UIColor blueColor], [UIColor purpleColor], nil];
    
    for (int i = 0; i < pageCount; i++) {
        
        CGRect aRect = CGRectMake(rect.origin.x + (i * rect.size.width), rect.origin.y,
                                  rect.size.width, rect.size.height);
        UIView *view1 = [[UIView alloc]initWithFrame:aRect];
        view1.backgroundColor = colors[i];
        [scrollView addSubview:view1];
    }
    
    [self moveToPage:3];
}

- (void) moveToPage:(int)index
{
    index--;
    CGRect rect = [[UIScreen mainScreen]bounds];
    CGPoint point = CGPointMake(rect.size.width * index, 0);
    [scrollView setContentOffset:point];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 拖拽完毕以后调用
    int index = [self getPageIndex:scrollView];
    NSLog(@"index = %d",index);
}

-(int) getPageIndex:(UIScrollView *)scrollView
{
    return fabs(scrollView.contentOffset.x) /  scrollView.frame.size.width;
}

@end

 

scrollview 例子2

标签:style   blog   color   io   ar   for   div   sp   log   

原文地址:http://www.cnblogs.com/code-style/p/3984605.html

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