//
// RootView.m
// UIScorllerView0805
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
import “RootView.h”
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubviews];
}
return self;
}
pragma mark - 添加控件方法
(void)addSubviews
{
self.backgroundColor = [UIColor cyanColor];// 向rootView上面添加scrollerView
// 1.创建scroller
self.scroller = [[UIScrollView alloc]initWithFrame:self.frame];self.scroller.maximumZoomScale = 3;
self.scroller.minimumZoomScale = .01;// 2.创建一个imageView
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”123.jpg”]];imageView.tag = 111;
// 3.把imageView贴到scrollerView上
[self.scroller addSubview:imageView];// 4.给scrolerView设置内容视图大小
// 要想让scrollerView能够滑动,必须让内容视图大于自身的boundsself.scroller.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
// 5.是否按整页来滑动(整页的宽度就是bounds的宽度)
// self.scroller.pagingEnabled = YES;// 6.是否滑动的开关(默认YES)
// self.scroller.scrollEnabled = NO;// 7.scrollerView的弹簧属性(默认YES)
// self.scroller.bounces = NO;// 8.scrollerView的指示条属性(默认YES)
// self.scroller.showsHorizontalScrollIndicator = YES;
// self.scroller.showsVerticalScrollIndicator = NO;self.scroller.delegate = self;
[imageView release];
[self addSubview:self.scroller];
[self.scroller release];
// self.scroller.contentOffset = CGPointMake(100, 100);
// self.scroller.showsHorizontalScrollIndicator = NO;
// NSLog(@”%ld” , self.scroller.subviews.count);
// 9.scrollerView内部偏移量
// self.scroller.contentInset = UIEdgeInsetsMake(100, 20, 400, 10);
// 创建一个button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(100, 400, 100, 30)];
[button setTitle:@"btn" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
pragma mark - scrollerView的缩放触发方法
(UIView )viewForZoomingInScrollView:(UIScrollView )scrollView
{
NSLog(@”&&&&&&&&”);
return [scrollView viewWithTag:111];
}(void)scrollViewDidEndZooming:(UIScrollView )scrollView withView:(UIView )view atScale:(CGFloat)scale
{
[scrollView viewWithTag:111].center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
NSLog(@”***“);
}
pragma mark - button点击事件
- (void)buttonAction:(UIButton *)sender
{
[UIView animateWithDuration:1 animations:^{
self.scroller.contentOffset = CGPointMake(375, 300);
}];
}
(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@”%@”, NSStringFromCGPoint(scrollView.contentOffset));
}(void)dealloc
{
[_scroller release];
[super dealloc];
}
@end
//
// RootView.m
// UIPageControl
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
import “RootView.h”
@implementation RootView
(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubviews];
}
return self;
}(void)addSubviews
{
self.backgroundColor = [UIColor brownColor];// 创建scrollerView
self.scroller = [[UIScrollView alloc]initWithFrame:self.frame];// for 创建6张图片
for (int i = 0; i < 6; i++) {
// 创建image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@”v6_guide_568h%d@2x.png” , i + 1]];// 创建imageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)]; imageView.image = image; // imageView添加到scrollerView上 [self.scroller addSubview:imageView]; [imageView release];
}
// 设置scrollerView的内容视图大小
self.scroller.contentSize = CGSizeMake(6 * self.frame.size.width, self.frame.size.height);// 设置整页滑动
self.scroller.pagingEnabled = YES;[self addSubview:self.scroller];
[self.scroller release];// 添加黑色布景
/*
self.blackView = [[UIView alloc]initWithFrame:self.frame];
self.blackView.backgroundColor = [UIColor blackColor];
[self addSubview:self.blackView];
[self.blackView release];
*/// 创建pageControl
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - kPageControlHeight, self.frame.size.width , kPageControlHeight)];self.pageControl.backgroundColor = [UIColor lightGrayColor];
// pageControl 默认 0 个点
// 给pageControl设置点的个数
self.pageControl.numberOfPages = 6;// 设置默认选中的点
// self.pageControl.currentPage = 2;// 当前选中的点的颜色
self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
// 未被选中的点的颜色
self.pageControl.pageIndicatorTintColor = [UIColor yellowColor];// 添加点击事件
// [self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
// self.pageControl.userInteractionEnabled = NO;
[self addSubview:self.pageControl];
[self.pageControl release];
}
pragma mark - pageControl点击事件
(void)pageControlAction:(UIPageControl *)sender
{
NSLog(@”%ld” , sender.currentPage);
}(void)dealloc
{
[_pageControl release];
[_blackView release];
[_scroller release];
[super dealloc];
}
@end
//
// RootViewController.m
// UIPageControl
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
import “RootViewController.h”
@interface RootViewController ()
@end
@implementation RootViewController
(void)loadView
{
[super loadView];
self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
}(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.// 给scrollerView设置点击事件
self.rootView.scroller.delegate = self;[self.rootView.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
pragma mark - pageControllerAction
(void)pageControlAction:(UIPageControl *)sender
{
// 1.获取到当前pageControl现实的下标
// NSInteger index = self.rootView.pageControl.currentPage;// 2.根据下标算出scrollerView的偏移量
// CGFloat offset = index * self.rootView.frame.size.width;// 3.根据偏移量 设置scrollerView的contentOffSet
// self.rootView.scroller.contentOffset = CGPointMake(offset, 0);self.rootView.scroller.contentOffset = CGPointMake(sender.currentPage * self.rootView.frame.size.width, 0);
}(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@”减速结束”);// 拿到偏移量
CGPoint offset = scrollView.contentOffset;// 算出偏移了几个frame.width
NSInteger currentIndex = offset.x / self.rootView.frame.size.width;//根据currentIndex 修改pageControl现实点的位置
self.rootView.pageControl.currentPage = currentIndex;
}
(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@”拖拽结束”);
}(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@”滑动中…..”);
}(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@”将要开始减速”);
}(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@”将要开始拖拽”);
}(void)scrollViewWillEndDragging:(UIScrollView )scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint )targetContentOffset
{
NSLog(@”将要结束拖拽”);
}
@end