UI1_UIScrollView

//
//  AppDelegate.m
//  UI1_UIScrollView
//
//  Created by zhangxueming on 15/7/10.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    ViewController *root = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
    self.window.rootViewController = nav;
    self.window.backgroundColor = [UIColor whiteColor];
    
    return YES;
}

 

//
//  ViewController.h
//  UI1_UIScrollView
//
//  Created by zhangxueming on 15/7/10.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@end

 

//
//  ViewController.m
//  UI1_UIScrollView
//
//  Created by zhangxueming on 15/7/10.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    imageView.image = image;
    //[self.view addSubview:imageView];
    //建立滾動視圖
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    //設置內容視圖的大小
    //跟imageView的大小相同
    scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    [scrollView addSubview:imageView];
    scrollView.backgroundColor = [UIColor redColor];
    //消除導航欄的影響,顯示的視圖不被導航欄覆蓋
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //隱藏滾動條
    //橫向的指示條
    scrollView.showsHorizontalScrollIndicator = NO;
    //豎向的指示條
    scrollView.showsVerticalScrollIndicator = YES;
    
    //設置邊界回彈
    scrollView.bounces = YES;
    //滾動視圖的偏移, 相對於內容視圖的偏移
    scrollView.contentOffset = CGPointMake(0, 0);
    //設置分頁
    //必然有一個減速的過程
    scrollView.pagingEnabled = YES;
    //是否滾動
    scrollView.scrollEnabled = YES;
    
    //[scrollView setContentOffset:CGPointMake(-100, -100) animated:YES];
    //[scrollView flashScrollIndicators];
    
    //設置放大縮小的係數
    scrollView.delegate = self;
    scrollView.minimumZoomScale  = 0.2;
    scrollView.maximumZoomScale  = 3;
    
    [self.view addSubview:scrollView];
}


#pragma mark --UIScrollViewDelegate---
//視圖發生滾動時調用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //NSLog(@"----滾動----");
}
//視圖發生拖拽的時候調用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"----拉拽----");
}

//拖拽結束時調用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"decelerate = %i", decelerate);
    if (decelerate) {
        NSLog(@"拖拽減速");
    }
}
//返回要放大子視圖
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSLog(@"圖片被放大");
    
    return [scrollView.subviews objectAtIndex:0];
}

//當有減速過程的時候才被調用
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"開始減速");
}
//若是設置分頁使能, 那麼該方法確定被調用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"減速結束");
}



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

@end
本站公眾號
   歡迎關注本站公眾號,獲取更多信息