UIKit 框架之UIScrollView

本文详细介绍了UIScrollView的使用方法,包括如何设置其基本属性如frame、contentSize等,并通过实例演示了UIScrollView中图片滚动的效果。同时,文章还讲解了UIScrollViewDelegate的相关方法及其应用场景。
摘要由CSDN通过智能技术生成

理解UIScrollView时可以假设这样的一个场景:在一个漆黑的屋子里的墙上有一副大的画,墙是可以上下左右滚动,一个矩形的手电筒固定着,开始时画的左上角坐标与矩形光的左上角坐标对应。人可以用手电筒的光来看这幅画。

 

//
//  ViewController.m
//  UIScrollView
//
//  Created by City--Online on 15/5/20.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //Frame类似矩形手电筒发出的光 眼睛每次只能看到矩形大小的东西
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 100, self.view.bounds.size.width, 100)];
    //画布
    _scrollView.contentSize=CGSizeMake(self.view.bounds.size.width*3, self.view.bounds.size.height);
    //画布里墙角的距离 类似CSS的盒子模型  contentSize、contentInset共同组成整块墙
    _scrollView.contentInset=UIEdgeInsetsMake(10, 10, 10, 10);
    //设置代理
    _scrollView.delegate=self;
    //指定控件是否只能在一个方向上滚动
    _scrollView.directionalLockEnabled=YES;
    //控制控件遇到边框是否反弹
    _scrollView.bounces=YES;
    //控制垂直方向遇到边框是否反弹
    _scrollView.alwaysBounceVertical=NO;
    //控制水平方向遇到边框是否反弹
    _scrollView.alwaysBounceHorizontal=NO;
    //控制控件是否整页翻动
    _scrollView.pagingEnabled=YES;
    //控制控件是否能滚动
    _scrollView.scrollEnabled=YES;
    //控制是否显示水平方向的滚动条
    _scrollView.showsHorizontalScrollIndicator=YES;
    //控制是否显示垂直方向的滚动条
    _scrollView.showsVerticalScrollIndicator=YES;
    //指定滚动条在scrollerView中的位置
    _scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(0, 50, 0, 0);
    //设定滚动条的样式
    _scrollView.indicatorStyle=UIScrollViewIndicatorStyleBlack;
    //减速速率
    _scrollView.decelerationRate=3.0;
    
    //    //设置墙左上角坐标移动到指定的Point
    //    [_scrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
    //    //滚动到指定矩形的边缘 使得该矩形可见
    //    [_scrollView scrollRectToVisible:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height) animated:YES];
    //显示滚动条,但是滚动条(ScrollBar Indicators) 只会显示大概1.5秒左右的时间,就会自动隐藏
    //    [_scrollView flashScrollIndicators];
    
    //如果用户已经触及的内容视图但可能还没有开始拖动它。这个属性的值便是YES
    BOOL IsTracking =_scrollView.tracking;
    NSLog(@"tracking=%d",IsTracking);
    
    //是否在被拖拽
    BOOL IsDragging=_scrollView.dragging;
    NSLog(@"dragging=%d",IsDragging);
    
    //是否在减速
    BOOL  IsDecelerating=_scrollView.decelerating;
    NSLog(@"decelerating=%d",IsDecelerating);
    
    //是否延时调用开始滚动的方法 if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:
    _scrollView.delaysContentTouches=YES;
    
    //是否接触取消touch的事件
    _scrollView.canCancelContentTouches=YES;
    
    //缩小的最小值
    _scrollView.minimumZoomScale=0.5;
    //放大的最大值
    _scrollView.maximumZoomScale=2;
    //当前缩放值
    _scrollView.zoomScale=1;
    
    //缩放到指定的大小
    //    - (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated
    //缩放到指定的Rect
    //    - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
    
    //控件的大小是否正在改变
    BOOL IsZooming= _scrollView.zooming;
    NSLog(@"zooming=%d",IsZooming);
    
    //设置缩放到边界时是否反弹
    _scrollView.bouncesZoom=YES;
    
    //是否进行缩放反弹
    BOOL IsZoomBouncing= _scrollView.zoomBouncing;
    NSLog(@"zoomBouncing=%d",IsZoomBouncing);
    
    //滚动到顶部
    BOOL IsScrollsTop= _scrollView.scrollsToTop;
    NSLog(@"scrollsToTop=%d",IsScrollsTop);
    
    //开始拖到时隐藏键盘
    _scrollView.keyboardDismissMode=UIScrollViewKeyboardDismissModeOnDrag;
    
    //获取拖动手势
    UIGestureRecognizer *panGestureRecognizer=_scrollView.panGestureRecognizer;
    //获取撮合手势
    UIGestureRecognizer *pinchGestureRecognizer=_scrollView.pinchGestureRecognizer;
    
    for (int i=0; i<3; i++) {
        UIImageView *imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
        imgView.frame=CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height) ;
        [_scrollView addSubview:imgView];
    }
    _scrollView.userInteractionEnabled=YES;
    _scrollView.scrollEnabled=YES;
    
    
    [self.view addSubview:_scrollView];
//    self.view.backgroundColor=[UIColor yellowColor];
    
}

//UIScrollViewDelegate
//视图滚动时触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScroll");
}
//视图缩放时触发
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidZoom");
}
//开始拖动时触发
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDragging");
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"scrollViewWillEndDragging");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"scrollViewDidEndDragging");
}

//开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDecelerating");
}
//减速结束 停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidEndDecelerating");
    
}
//结束滚动动画调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{}
//返回要缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return nil;
}
//开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    NSLog(@"scrollViewWillBeginZooming");
}
//缩放完成
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    NSLog(@"scrollViewDidEndZooming");
}

//是否允许滚动到顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewShouldScrollToTop");
    return YES;
}
//滑动到顶部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScrollToTop");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值