//
// RootViewController.m
// LessonUIScrollView
//
// Created by lanouhn on 15/3/26.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController () <UIScrollViewDelegate> {
UIButton *button;
UIImageView *newImageView;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
//UIScrollView, 滚动视图类,继承于UIView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
scrollView.tag = 100;
//frame决定了可视范围, contentSize决定内容页的大小,
//如果contentSize小于frame, 内容不足一页, scrollView不能滚动,
//如果contentSize大于frame, 内容超过一页, scrollView才能滚动
//设置内容大小,内容宽度超过frame.width, 支持水平滚动; 内容高度超过frame.height, 支持垂直滚动
scrollView.contentSize = CGSizeMake(1000, 1000);
//能否滚动
scrollView.scrollEnabled = YES;
//是否显示水平滚动条
scrollView.showsHorizontalScrollIndicator = NO;
//是否显示上下滚动条
scrollView.showsVerticalScrollIndicator = YES;
//YES, 如果开始时时 水平或者垂直, 只支持一个方向滚动, 如果开始时是 对角线方向, 支持水平和垂直同时滚动
scrollView.directionalLockEnabled = YES;
//取消回弹
scrollView.bounces = YES;
//当contentSize.height < frame.height, 垂直方向是否支持回弹
scrollView.alwaysBounceVertical = YES;
//当contentSize.width < frame.width, 水平方向是否支持回弹
scrollView.alwaysBounceHorizontal = NO;
//点击状态栏回到顶部
scrollView.scrollsToTop = YES;
//内容偏移量
scrollView.contentOffset = CGPointMake(200, 0);
//注:必须配合delegate才能使用
//最小缩放比例
scrollView.minimumZoomScale = 0.5;
//最大缩放比例
scrollView.maximumZoomScale = 2;
//缩放比例
scrollView.zoomScale = 2;
//设置delegate
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:scrollView];
[scrollView release];
button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 175, 40);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"按钮" forState:UIControlStateNormal];
[scrollView addSubview:button];
UIButton *topButton = [UIButton buttonWithType:UIButtonTypeSystem];
topButton.frame = CGRectMake(325, 20, 50, 50);
topButton.layer.borderWidth = 5;
topButton.layer.borderColor = [UIColor greenColor].CGColor;
topButton.layer.cornerRadius = 25;
[topButton addTarget:self action:@selector(top) forControlEvents:UIControlEventTouchUpInside];
[topButton setTitle:@"置顶" forState:UIControlStateNormal];
[self.view addSubview:topButton];
newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"路飞"]];
newImageView.frame = CGRectMake(0, 0, 375, 667);
[scrollView addSubview:newImageView];
[newImageView release];
//UIPageControl, 页码控制器, 继承于UIControl
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 375, 40)];
pageControl.backgroundColor = [UIColor blackColor];
//设置页码
pageControl.numberOfPages = 10;
//设置当前页数, 从0开始,
pageControl.currentPage = 1;
//如果只有一页,是否隐藏pageControl
pageControl.hidesForSinglePage = YES;
//小点点的颜色
pageControl.pageIndicatorTintColor = [UIColor redColor];
//当前小点点的颜色
pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
[self.view addSubview:pageControl];
[pageControl release];
}
- (void)top {
UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:100];
//滚动到顶部
// scrollView.contentOffset = CGPointZero;
[scrollView setContentOffset:CGPointZero animated:YES];
//滚动到某个区域
[scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
//滚动到底部
CGFloat height = scrollView.contentSize.height - scrollView.frame.size.height;
[scrollView setContentOffset:CGPointMake(0, height) animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIScrollViewDelegate
//进行缩放, 调用此方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
}
//对哪个代理进行缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return newImageView;
}
//开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
NSLog(@"%s", __FUNCTION__);
NSLog(@"%@", view);
}
//结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
NSLog(@"%s", __FUNCTION__);
NSLog(@"%@", view);
//结束
NSLog(@"%.2lf", scale);
}
//视图发生了滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
}
//将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}
//将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
}
//已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
}
//将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
}
//已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
}
//是否允许滚动到顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
return YES;
}
//已经滚动到顶部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
}
@end
###############
//
// WelcomeViewController.m
// LessonUIScrollView
//
// Created by lanouhn on 15/3/26.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
#import "WelcomeViewController.h"
@interface WelcomeViewController () <UIScrollViewDelegate>
{
UIScrollView *scrollView;
UIPageControl *pageControl;
}
@end
@implementation WelcomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
CGRect screenSize = [UIScreen mainScreen].bounds;
scrollView = [[UIScrollView alloc] initWithFrame:screenSize];
scrollView.contentSize = CGSizeMake(screenSize.size.width * 4, screenSize.size.height);
//按页滚动
scrollView.pagingEnabled = YES;
//隐藏水平滚动条
scrollView.showsHorizontalScrollIndicator = NO;
//设置代理
scrollView.delegate = self;
[self.view addSubview: scrollView];
[scrollView release];
for (NSInteger i = 0; i < 4; i++) {
NSString *name = [NSString stringWithFormat:@"1%ld", i + 1];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
imageView.frame = CGRectMake(i * screenSize.size.width, 0, screenSize.size.width, screenSize.size.height);
[scrollView addSubview:imageView];
[imageView release];
}
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 600, 375, 40)];
pageControl.numberOfPages = 4;
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
// pageControl.backgroundColor = [UIColor blackColor];
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
[self.view addSubview:pageControl];
[pageControl release];
//创建跳过button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(325, 20, 50, 50);
[button setTitle:@"跳过" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pass) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//创建返回button
UIButton *returnbutton = [UIButton buttonWithType:UIButtonTypeSystem];
returnbutton.frame = CGRectMake(0, 20, 50, 50);
[returnbutton setTitle:@"返回" forState:UIControlStateNormal];
[returnbutton addTarget:self action:@selector(return1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:returnbutton];
//创建登录button
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(self.view.frame.size.width * 3 + 88, 480, 200, 40);
loginButton.backgroundColor = [UIColor greenColor];
[loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
[loginButton setTitle:@"登录" forState:UIControlStateNormal];
[scrollView addSubview:loginButton];
}
- (void)changePage:(UIPageControl *)aPageControl {
//偏移量
CGPoint point = CGPointMake(aPageControl.currentPage * self.view.frame.size.width, 0);
//修改偏移量(带动画0
[scrollView setContentOffset:point animated:YES];
}
- (void)pass {
pageControl.currentPage = 3;
//偏移量
CGPoint point = CGPointMake(pageControl.currentPage * self.view.frame.size.width, 0);
//修改偏移量(带动画0
[scrollView setContentOffset:point animated:YES];
}
- (void)return1 {
pageControl.currentPage = 0;
//偏移量
CGPoint point = CGPointMake(pageControl.currentPage * self.view.frame.size.width, 0);
//修改偏移量(带动画0
[scrollView setContentOffset:point animated:YES];
}
- (void)login:(UIButton *)aButton {
// UIAlertView *alView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登录成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
// [alView show];
// [alView release];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1 {
// NSLog(@"%s", __FUNCTION__);
pageControl.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end