关闭

UI_UIScrollView/UIpageControl

标签: scrollview滑动pagecontrliosui
424人阅读 评论(0) 收藏 举报
分类:

创建UIScrollView

@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIImageView *opeImageView;
@property (nonatomic, retain)UIPageControl *pageControl;
#pragma mark - 创建滑动视图  UIScrollView
- (void)createScrollView
{
    self.scrollView = [[[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.backgroundColor = [UIColor grayColor];

    // contentSize 决定了scrollView 滑动范围的大小
    _scrollView.contentSize = CGSizeMake(6 * KScreenWidth, KScreenWidth);

    // 是否显示滚动条 默认显示
    self.scrollView.showsHorizontalScrollIndicator = YES; // 水平
    self.scrollView.showsVerticalScrollIndicator = YES;  // 竖直

    // 整页反动 默认 NO
    _scrollView.pagingEnabled = YES;

    // 回弹效果 默认 YES
    _scrollView.bounces = YES;

    // 设置 scrollView 的偏移量 默认是 (0,0)
    _scrollView.contentOffset = CGPointMake(KScreenWidth, 0);

    // 点击滚动条回到顶部
    _scrollView.scrollsToTop = YES;

    // 是否允许拖动 默认 YES
    _scrollView.scrollEnabled = YES;

    [self addSubview:_scrollView];
    [self.scrollView release];
}

创建UIPageControl

#pragma mark - 创建 UIPageControl
- (void)createPageControl
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(120, KScreenHeight - 100, 160, 30)];

    // 设置小点的个数
    self.pageControl.numberOfPages = 6;

    // 设置小点颜色
    self.pageControl.pageIndicatorTintColor = [UIColor redColor];

    // 改变选中小点的颜色
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    [self addSubview:self.pageControl];
    [self.pageControl release];
}

创建引导多张图片

#pragma mark - 创建引导多张图片
- (void)createLeadPageImage
{
    for (int i = 0; i < 5; i++) {

        NSString *name = [NSString stringWithFormat:@"bg_%d.jpg",i];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
        imageView.frame = CGRectMake(i * KScreenWidth, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:imageView];
        [imageView release];
    }
}

创建 ImageView

#pragma mark - 创建 ImageView
- (void)createOpeImageView
{
    self.opeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_4"]];
    self.opeImageView.frame = CGRectMake(0, 0, 375, 667);
    [self.scrollView addSubview:_opeImageView];
    [_opeImageView release];
}

实现代理

@interface RootViewController () <UIScrollViewDelegate>
- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"%@", self.view);


    // 设置scrollView 的代理
    self.rootView.scrollView.delegate = self;

    // 设置最大的缩放比例
    self.rootView.scrollView.maximumZoomScale = 3;

    // 设置最小的缩放比例
    self.rootView.scrollView.minimumZoomScale = 0.3;
}

代理方法

#pragma mark - scroolView 滑动的时候会触发方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"滑动");

    // 图片带动小点移动
    NSInteger index =  self.rootView.scrollView.contentOffset.x / KScreenWidth;
    self.rootView.pageControl.currentPage = index;
    // pageControl 绑定监听事件
    [self.rootView.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
}
// 实现 pageControl 绑定监听事件

- (void)pageControlAction:(UIPageControl *)sender
{

    [self.rootView.scrollView setContentOffset:CGPointMake(KScreenWidth * sender.currentPage, 0) animated:YES];
}
#pragma mark - scroolView 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"开始拖拽了");
}

#pragma mark - scroolView 结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"结束拖拽");
}


#pragma mark - scroolView 开始减速触发方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"开始减速触发方法");
}


#pragma mark - scroolView 结束减速触发方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"结束减速����");
}

缩放

#pragma mark - scrollView 缩放执行过程中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    if (self.rootView.scrollView.zoomScale < 1.0) {
        self.rootView.opeImageView.center = _rootView.center;
    }
}


#pragma mark - scroolView 指定缩放的内容
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.rootView.opeImageView;
}
0
0

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