关闭

[TwistedFate]UIScrollView UIPageControl

标签: 轮播图
156人阅读 评论(0) 收藏 举报
分类:

UIScrollView

初始化及添加背景色

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
scrollView.backgroundColor = [UIColor cyanColor];

设置显示内容区域

scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 6);

关闭滚动条

scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;

整页滑动

scrollView.pagingEnabled = YES;

设置回弹效果(单独设置没有效果,三个一起设置)

scrollView.bounces = YES;
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;

设置缩放比例

scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2;

设置代理

scrollView.delegate = self;

显示视图及释放内存

[self.view addSubview:scrollView];
[scrollView release];

添加图片

UIImageView *imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageV.image = [UIImage imageNamed:@"1.jpg"];
imageV.tag  = 100;
// 注意:把图片加到scrollView上
[scrollView addSubView:imageV];
[imageV release];

添加还原缩放的按钮

UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor  greenColor];
[button addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];

实现button的方法

- (void)click:(UIButton *)button{
    UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:101];
    // 重置一下缩放比例
    [scroll setZoomScale:1 animated:YES];
    // 改变一下偏移量
    [scroll setContentOffset:CGPointMake(0, kScreenHeight)animated: YES];
}

UIScrollViewDelegate的方法实现

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //NSLog(@"滑动时一直触发");
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    // 缩放时按中心点来缩放
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
    // 改变image的中心点 始终等于self.view的中心点
    imageView.center = self.view.center;
    NSLog(@"缩放时 一直触发");
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    // 指定要缩放的视图 并且是scrollView的子视图
//    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
//    return imageView;
    return scrollView.subviews[0];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"将要开始拖动");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"已经结束拖拽");
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    NSLog(@"将要开始减速");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 最终图片停止在这个方法 ******************
    NSLog(@"已经结束减速");
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"缩放已经结束触发");
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    // 改变偏移量的动画结束后触发
    NSLog(@"改变偏移量的动画结束后触发");
}

UIPageControl

初始化

UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50)];
pageControl.backgroundColor = [UIColor blackColor];

设置共多少页

pageControl.numberOfPages = 5;

设置当前显示第几页

pageControl.currentPage = 2;

设置选中和未选中的颜色

pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];

添加单击响应事件

[pageControl addTarget:self action:@selector(page:) forControlEvents:(UIControlEventTouchUpInside)];

添加显示视图

// 不能加在图片上 否则会滑走
[self.view addSubview:pageControl];
[pageControl release];

实现单机响应事件方法

- (void)page:(UIPageControl *)page{
    NSLog(@"%ld",page.currentPage);   
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13549次
    • 积分:751
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:4篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论