滚动视图及缩放

import "AppDelegate.h"

import "RootViewController.h"

import "ZoomViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    RootViewController *rootVC = [[RootViewController alloc]init];
    [self.window setRootViewController:rootVC];

    ZoomViewController *zoomVC = [[ZoomViewController alloc]init];
    [self.window setRootViewController:zoomVC];

    return YES;
    }


import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>

@end

@implementation RootViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];

    // 创建滚动视图 当我们显示的内容超过一屏时,就需要用到滚动视图
    UIScrollView scroolView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    scroolView.tag = 1000;
    scroolView.backgroundColor = [UIColor whiteColor];
    // 设置滚动区域
    [scroolView setContentSize:CGSizeMake(CGRectGetWidth(self.view.frame)
    3, CGRectGetHeight(self.view.frame))];
    // 为scroll添加子视图(设置为4屏)
    // NSArray *colorArr = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor whiteColor],[UIColor greenColor], nil];
    NSArray *imageArr = [NSArray arrayWithObjects:@"0.png",@"1.png",@"2.png", nil];
    for (int i = 0; i < 3; i++) {

      UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
      imageView.image = [UIImage imageNamed:imageArr[i]];
      imageView.userInteractionEnabled = YES;
      [scroolView addSubview:imageView];
    
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
      label.text = [NSString stringWithFormat:@"这是第%d个视图",i];
      label.backgroundColor = colorArr[i];
      label.textAlignment = NSTextAlignmentCenter;
      label.font = [UIFont systemFontOfSize:60];
      [scroolView addSubview:label];
    

    }
    [self.view addSubview:scroolView];
    // 设置分页效果(默认值为NO)
    scroolView.pagingEnabled = YES;
    // 设置横向滚动条是否显示(默认值为YES)
    scroolView.showsHorizontalScrollIndicator = YES;

    // 设置边界是否有反弹效果(默认值为YES)
    scroolView.bounces = NO;
    // 设置代理
    scroolView.delegate = self;

    // 初始化UIPageControl
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 40)];
    pageControl.tag = 1001;

    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    // 设置页数
    [pageControl setNumberOfPages:3];
    // 设置当前所在页
    pageControl.currentPage = 1;
    // 添加触发事件
    [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [pageControl setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:pageControl];

}

//pageControl的回调方法
-(void)pageAction:(UIPageControl)sender{
// 根据当前的页数,使得scrollView也滑动到对应的子视图 通过设置scrollView的contentOffset来实现(改变contentOffset实际上就是改变scrollView的bounds)
// 得到当前的页数
int page = (int)sender.currentPage;
// 根据当前页数来计算偏移量
// 得到scrollView
UIScrollView scrollView = (UIScrollView)[self.view viewWithTag:1000];
// 设置scrollView的偏移量 当前的页数乘以屏幕宽度
[scrollView setContentOffset:CGPointMake(page
CGRectGetWidth(self.view.frame), 0) animated:YES];
}

pragma mark --- 滚动视图的代理方法

// 开始拖拽(手指触碰到屏幕,并且移动)
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"-------%s",func);
}

// 已经开始滚动(只要scrollView是滚动状态就会调用次方法)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 滚动视图上面的子视图可以移动,不是子视图改变自身的frame值,而是通过更改滚动视图(也就是子视图的父视图)的bounds的origin(也就是x,y)来更改子视图在父视图(滚动视图)上显示的位置
// scrollView的偏移量
CGPoint offSet = scrollView.contentOffset;
NSLog(@"_____%f",offSet.x);
// 得到scrollView的bounds的x点
float x = scrollView.bounds.origin.x;
NSLog(@"bounds********%f",x);
NSLog(@"--------%s",func);
}

// 停止拖拽 当手指(触摸对象)离开,正在滚动的视图减速
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"_____%s",func);
}

// 视图真正静止(视图不动了)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 根据scrollView的偏移量来计算当前的页数
int page = (int)scrollView.contentOffset.x/CGRectGetWidth(self.view.frame);
// 得到pageControl
UIPageControl pageControl = (UIPageControl)[self.view viewWithTag:1001];
// 设置当前的页数
pageControl.currentPage = page;

NSLog(@"%s",__func__);

}

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

/*

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

import "ZoomViewController.h"

@interface ZoomViewController ()<UIScrollViewDelegate>

@end

@implementation ZoomViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    // 初始化scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    [scrollView setBackgroundColor:[UIColor orangeColor]];
    // 设置scrollView代理
    scrollView.delegate = self;
    // 设置缩放比例
    // 设置可缩小到的最小比例
    scrollView.minimumZoomScale = 0.5;
    // 设置可放大到的最大比例
    scrollView.maximumZoomScale = 2.0;

    [self.view addSubview:scrollView];
    // 添加图片,使得添加的图片宽高成比例
    UIImage myImage = [UIImage imageNamed:@"3.png"];
    // 得到图片的原始宽高
    float imageWidth = myImage.size.width;
    float imageHeight = myImage.size.height;
    // 这里规定imageView的宽为200,根据此宽度,得到等比例的高度
    float imageViewWidth = 200;
    float imageViewHeight = 200
    (imageHeight/imageWidth);
    // 初始化UIImageView
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageViewWidth, imageViewHeight)];
    imageView.tag = 1000;
    // 为imageView设置图片
    imageView.image = myImage;
    // 让imageView居中
    imageView.center = self.view.center;
    [scrollView addSubview:imageView];

}

pragma mark --- 滚动视图与缩放有关的代理方法

// 指定scrollView的某一个子视图为可缩放视图,前提条件是此视图已经添加到scrollView上面
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
UIImageView imageView = (UIImageView)[scrollView viewWithTag:1000];
return imageView;
}

// 开始缩放 第二个参数view:是指我们将要缩放的视图(这里就是imageView)
-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
NSLog(@"%@",view);
}

// 正在缩放 只要正在缩放就会执行此方法,所以此方法在放缩过程中会多次调用
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{

//  在缩放过程中,为了使得该子视图一直在屏幕中间位置,所以我们需要在它缩放过程中一直调整他的center
//  得到scrollView的子视图
UIImageView *imageView = (UIImageView*)[scrollView viewWithTag:1000];
//  打印imageView的frame。分析为什么它的位置会改变
NSLog(@"frame___%@",NSStringFromCGRect(imageView.frame));

//  设置imageView的center,使得它的位置一直在屏幕中间
imageView.center =self.view.center;

//  打印contentSize  分析为什么缩放之后会滑动
NSLog(@"contentSize------%@",NSStringFromCGSize(scrollView.contentSize));

}

// 缩放结束
// 第二个参数 : 当前正在缩放的视图
// 第三个参数 : 当前正在缩放视图的缩放比例
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
// 缩放完成之后恢复原大小,这里使用的是2D方式变换函数中与捏合有关的函数
// view.transform = CGAffineTransformMakeScale(1, 1);
}

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

/*

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容

  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 3,321评论 0 15
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,467评论 1 14
  • 掌握 UIScrollView的常见属性 UIScrollView的常用代理方法 UIScrollView的缩放 ...
    JonesCxy阅读 2,707评论 1 12
  • 梦里萦回佳人笑 醉醒无声空自扰 辗转寂寂无眠夜 只看窗雨笑秋风
    优婆塞阅读 297评论 0 0
  • 公元1992年11月11日,一个新的生命来到这个世界,他的奶奶顺次给他取名叫做叶凌辉,从此便在这个平凡的农民大家庭...
    iiKris阅读 354评论 0 1