day11---UIScrollView

UIScrollView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    
    //当滚动视图和导航控制器在一起的时候,滚动视图就会受其影响,那么设置一下属性,关闭对其影响(iOS7之后出现的)
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    /************* 创建UIScrollView *******************/
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, screenBounds.size.width, screenBounds.size.height-64)];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollView];
    
    
    UIImage *image = [UIImage imageNamed:@"1001.jpg"];
    //设置imgView的宽高和图片image的宽高一样
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    imgView.image = image;
    //把要展示的图片添加到scrollView上
    [scrollView addSubview:imgView];
    
    
    //***************  重点属性  ***********//
    //1.设置滚动的内容视图的滚动范围              【**重点**】
    scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);           (滑动图片首先设置视图滚动范围)
    //2.关闭弹簧效果
    scrollView.bounces = YES;
    //3.设置滚动的偏移量                        【** 重点 **】
    //x值为正数,视图往右边偏移,x值为负数,视图往左边偏移;
    //y值为正数,视图往下边偏移,y值为负数,视图往上边偏移;
    scrollView.contentOffset = CGPointMake(image.size.width-screenBounds.size.width, image.size.height-screenBounds.size.height);
    
    //4.在scrollView的四周增加额外的滚动区域【了解】
    scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
    //5.隐藏滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    //6.设置是否滚动
    scrollView.scrollEnabled = YES;
    
    //7.关于缩放系数的设置       (缩放图片首先设置视图的缩放系数)
    //设置内容视图的最小缩放系数
    ⁃       scrollView.minimumZoomScale = 0.03;
    //设置内容视图的最大缩放系数
    scrollView.maximumZoomScale = 2;
}

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



#pragma mark - UIScrollViewDelegate

//**** 重点 ***//
//1.当对scrollView将要开始进行拖拽的时候,调用;
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    NSLog(@"WillBeginDragging");

}


//2.scrollView滚动的时候,调用此方法(被调用若干次)     【** 重点 **】
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"DidScroll");
}


//3.结束对scrollView的拖拽是,调用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//    NSLog(@"DidEndDragging");
}


//4.scrollView停止滚动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{;
//    NSLog(@"DidEndDecelerating");
}



/*********** 关于缩放 ***************/
//1.设置scrollView中要进行缩放的内容视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews lastObject];
}


//2.将要开始缩放的时候调用
-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    NSLog(@"villBeginZooming");
}


//3.正在缩放的时候调用
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"DidZoom");
}


//4.缩放完毕的时候调用
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    NSLog(@"DidEndZooming");
}

你可能感兴趣的:(day11---UIScrollView)