zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    关于SUID、SGID、Sticky(转载)
    对Linux下TCP连接相关配置的优化记录(转载)
    ACL权限设置(转载)
    如何把一个命令加入到某个用户sudo的列表中(转载)
    菜鸟的逆袭 目标
    菜鸟的逆袭 自我介绍
    Weekly Work from表单练习
    CSS属性display的值及其描述
    CSS Position
    电源防反接保护电路
  • 原文地址:https://www.cnblogs.com/code-style/p/3984605.html
Copyright © 2011-2022 走看看