scrollview 例子2

代码:

#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

 

转载于:https://www.cnblogs.com/code-style/p/3984605.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值