美文网首页
UIScrollViewDelegate 内容缩放

UIScrollViewDelegate 内容缩放

作者: solozyx | 来源:发表于2016-08-15 17:30 被阅读43次

    1.任何NSObject对象实现 <UIScrollViewDelegate>协议 都可以作UIScrollView的代理对象

    创建ZYXDog类作UIScrollView的代理
    ZYXDog.h

    #import <UIKit/UIKit.h>
    
    @interface ZYXDog : NSObject <UIScrollViewDelegate>
    
    @end
    

    ZYXDog.m

    #import "ZYXDog.h"
    
    @implementation ZYXDog
    
    #pragma mark - <UIScrollViewDelegate>
    /**
     * 即将开始拖拽的时候调用
     */
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        NSLog(@"%s",__func__);
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        NSLog(@"%s",__func__);
    }
    
    
    /**
     * 结束拖拽的时候调用
     */
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                      willDecelerate:(BOOL)decelerate{
        NSLog(@"%s",__func__);
    }
    
    /**
     * (减速完毕)由于惯性停止滚动的时候调用
     */
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        NSLog(@"%s",__func__);
    }
    @end
    
    //***开始拖动UIScrollView时调用
    // -[ZYXDog scrollViewWillBeginDragging:]
    //***在滚动时调用UIScrollView
    // -[ZYXDog scrollViewDidScroll:]
    //***手指拖动UIScrollView滚动没有滚动到UIScrollView的frame边缘手指减速,UIScrollView停止滚动调用
    // -[ZYXDog scrollViewDidEndDragging:willDecelerate:]
    
    
    // -[ZYXDog scrollViewWillBeginDragging:]
    // -[ZYXDog scrollViewDidScroll:]
    //***手指拖动UIScrollView滚动
    //***滚动到UIScrollView的frame边缘极限UIScrollView被迫停止滚动调用
    // -[ZYXDog scrollViewDidEndDragging:willDecelerate:]
    //***由于UIScrollView的弹簧效果UIScrollView反弹继续滚动
    // -[ZYXDog scrollViewDidScroll:]
    //***在没有手指作用下UIScrollView停止滚动调用
    // -[ZYXDog scrollViewDidEndDecelerating:]
    

    ViewController.m

    #import "ViewController.h"
    #import "ZYXDog.h"
    
    @interface ViewController () 
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
    /** dog */
    @property (nonatomic, strong) ZYXDog *dog;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]];
        [self.scrollview addSubview:imageView];
        self.scrollview.backgroundColor = [UIColor redColor];
        self.scrollview.contentSize = imageView.image.size;
        // 设置代理
        self.dog = [[ZYXDog alloc] init];
        self.scrollview.delegate = self.dog;
    }
    @end
    

    2.内容缩放

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController () <UIScrollViewDelegate>
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
    @property (weak, nonatomic) UIImageView *imageView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
            
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]];
        [self.scrollview addSubview:imageView];
        self.imageView = imageView;
        
        self.scrollview.backgroundColor = [UIColor redColor];
        self.scrollview.contentSize = imageView.image.size;
        
        // 设置代理
        self.scrollview.delegate = self;
        // 设置缩放比例
        self.scrollview.maximumZoomScale = 2.0;
        self.scrollview.minimumZoomScale = 0.2;
    }
    
    #pragma mark - <UIScrollViewDelegate>
    /**
     这个方法的返回值决定了要缩放的内容(返回值只能是UIScrollView的子控件)
     */
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        return self.imageView;
    }
    
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView{
        NSLog(@"缩放ing-----%f", scrollView.zoomScale);
    }
    
    @end
    

    真机调试 可以实现内容缩放

    相关文章

      网友评论

          本文标题:UIScrollViewDelegate 内容缩放

          本文链接:https://www.haomeiwen.com/subject/kcbusttx.html