//
// ViewController.m
// UI05_ScrollView
//
// Created by lanou3g on 17/8/9.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 200, 400)];
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
self.scrollView = scrollView;
//设置scrollView的滑动范围(内容的实际尺寸)
scrollView.contentSize = CGSizeMake(800, 800); // 可在区域范围内左右上下移动
//设置scrollView的偏移量(x控制水平方向,y控制垂直方向)
scrollView.contentOffset = CGPointMake(0, 100);
//设置是否以动画的形式偏移
[scrollView setContentOffset:CGPointMake(0, 100) animated:YES];
//是否能触壁反弹(默认值是YES)
scrollView.bounces = NO;
//在scrollView的周围补充额外的滑动区域(上左下右)
scrollView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
//锁定滑动方向
scrollView.directionalLockEnabled = YES;
//垂直没有回弹
// scrollView.alwaysBounceVertical = YES;
//默认值是NO,当该属性值为YES,且bounces为YES,在水平方向的滚动范围小于scrollView的bounces的宽度的时候,允许出现水平回弹效果
//水平没有回弹
scrollView.alwaysBounceHorizontal = YES;
//以整页翻动(水平方向滑动每次为一个scrollView的frame宽度,垂直方向滑动每次为一个scrollView的frame的高度)
scrollView.pagingEnabled = YES;
//设置scrollView能否以动画的形式偏移
// scrollView.scrollEnabled = NO;
//关闭垂直方向的滑动指示器
// scrollView.showsVerticalScrollIndicator = NO;
//关闭水平方向的滑动指示器
// scrollView.showsHorizontalScrollIndicator = NO;
//滑动指示器样式
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//控制缩放的范围
scrollView.delegate = self;
scrollView.minimumZoomScale = 0.5f;
scrollView.maximumZoomScale = 2.0f;
//设置scrollview缩放倍数
// scrollView.zoomScale = 1.0;
//轻拍回到顶部
scrollView.scrollsToTop = YES;
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
greenView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:greenView];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(50, 300, 300, 400)];
yellowView.backgroundColor = [UIColor yellowColor];
[scrollView addSubview:yellowView];
}
//视图滑动时调用该方法 *****
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// NSLog(@"scroll offset x:%lf, y:%lf",scrollView.contentOffset.x,scrollView.contentOffset.y);
//偏移的透明度
[[scrollView.subviews firstObject] setAlpha: scrollView.contentOffset.y/100];
//判断是否要滑动到底部
if (scrollView.contentSize.height-scrollView.contentOffset.y-scrollView.frame.size.height <= 50) {
NSLog(@"还有不到50个点就要滑动到底部");
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subviews firstObject];
}
//开始拖5
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
NSLog(@"------------drag begin");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"drag end");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
NSLog(@"decelerate begin");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"decelerate end");
}
- (void)viewDidAppear:(BOOL)animated {
[self.scrollView setContentOffset:CGPointMake(100, 400) animated:YES];
}
//当我们调用setcontentoffset:animated:方法时,触发偏移动画,该动画结束时(到达偏移位置时)会调用此方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"scroll animation end");
}
//缩放时调用方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(@"zoom");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UI05_ScrollView(17-08-09)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 小学语文修改病句的方法 修改病句是小学语文考试中常见的题型,在修改病句之前,我们应该清晰的了解有哪些病句现象,下面...
- 一、本周践行完成情况: 1.早起践行:完成良好 2.运动践行:完成良好 适身体情况而定。 3.阅读学习:完成一般 ...