iOS开发之 UI 编程——第七讲

UIScrollView和UIPageControl配合使用

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    // 设置背景色
    self.view.backgroundColor = [UIColor grayColor];

    // 添加UIScrollView
    self.userGuideScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)] autorelease]; // 初始化位置和大小
    self.userGuideScrollView.contentSize = CGSizeMake(self.view.frame.size.width* 11, self.view.frame.size.height); // 设置存放的内容的大小
    _userGuideScrollView.pagingEnabled = YES; // 设置scrollView按一整页滑动
    _userGuideScrollView.delegate = self; // 设置代理为当前
    _userGuideScrollView.userInteractionEnabled = YES; // 设置可以与用户交互
    _userGuideScrollView.showsHorizontalScrollIndicator = YES; // 显示滑动时候横向的滚动条
    // 添加图片到UIScrollView中
    for (int i = 1; i <= 11; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:[NSString stringWithFormat:@"image%d", i]]];
        imageView.frame = CGRectMake((i - 1) * self.view.frame.size.width, 0,self.view.frame.size.width, self.view.frame.size.height);
        [_userGuideScrollView addSubview:imageView];
        [imageView release], imageView = nil;
    }
    // 添加到当前的View
    [self.view addSubview:_userGuideScrollView];


    // 初始化UIPageController
    self.scrollPageControl = [[[UIPageControl alloc] init] autorelease];
    _scrollPageControl.frame = CGRectMake(0, self.view.frame.size.height - 60,self.view.frame.size.width, 30); // 设置位置
    _scrollPageControl.numberOfPages = 11; // 设置有多少个小点点
    _scrollPageControl.currentPage = 0; // 设置当前是哪个点点
    _scrollPageControl.pageIndicatorTintColor = [UIColor blueColor]; // 没有选中的点点的颜色
    _scrollPageControl.currentPageIndicatorTintColor = [UIColor yellowColor]; //当前点点的颜色
    [_scrollPageControl addTarget:self action:@selector(scrollPageControlAction:)forControlEvents:UIControlEventValueChanged]; // 设置监听事件
    _scrollPageControl.highlighted = YES;
    [self.view addSubview:_scrollPageControl];

}

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


#pragma mark - 实现---scrollPageControlAction:监听事件
- (void)scrollPageControlAction:(UIPageControl *) sender {
    //令UIScrollView做出相应的滑动显示
    CGSize viewSize = _userGuideScrollView.frame.size;
    CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
    [_userGuideScrollView scrollRectToVisible:rect animated:YES];
}


#pragma mark - 重写----scrollViewDelegate中的代理方法 实现滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 计算偏移量
    int index = abs(scrollView.contentOffset.x / scrollView.frame.size.width);
    // 根据偏移量来设置pageControl
    _scrollPageControl.currentPage = index;
}


#pragma mark - 重写----dealloc方法
- (void)dealloc {
    // 释放属性的内存
    [_scrollPageControl release], _scrollPageControl = nil;
    [_userGuideScrollView release], _userGuideScrollView = nil;
    // 调用父类的dealloc方法
    [super dealloc];
}


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值